java - 关于angularjs向后台请求数据以及解析的问题
PHP中文网
PHP中文网 2017-04-18 09:19:23
0
3
371

我在angularjs的controller中发送了http请求,请求到后台能够得到返回数据,可是响应是404,data为undefined.
js文件是加载到infoUpdate.html页面中,每次页面中的controller创建后,会自动调用js中的controller,发送http请求到java后台。

下面是js代码:

angular.module('infoUpdate',[])
.controller('infoUpdateController',function($scope,$http){
    $scope.subscriberData = {},
    $http({
        method: 'GET',
        url: 'http://localhost:8080/Weixin/getSubscriberInfo?subscriberOpenId=ojXQbxLbEY9wnrFgnL5ODCyVVpnc'
    }).success(function(data,status){
       /* $scope.subscriberData.phone = data.phone;
        $scope.subscriberData.email = data.email;
        $scope.subscriberData.kindleEmail = data.kindleEmail;*/
        alert("findInfo successfully! phone:"+ data.email);
    }).error(function(data,status){
        alert("findInfo error! data.phone:" + data.phone + " status:" + status);
    })
});

后台java相关的代码如下:

package com.kindlepocket.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kindlepocket.web.pojo.HttpResult;
import com.kindlepocket.web.service.SubscriberService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.kindlepocket.web.service.TextBookInfoSearchService;
import com.kindlepocket.web.util.CheckUtil;
import com.kindlepocket.web.util.MessageUtil;

@Controller
@RequestMapping("/Weixin")
public class KindlePocketController {

    private static final long serialVersionUID = 1L;

    private static String SUBSCRIBER_OPENID = null;

    @Autowired
    private TextBookInfoSearchService searchService;

    @Autowired
    private SubscriberService ssbService;

    private static Logger logger = Logger.getLogger(KindlePocketController.class);

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @RequestMapping(value = "/getSubscriberInfo", method=RequestMethod.GET)
    private String getSubscriberInfo(@RequestParam("subscriberOpenId")String subscriberOpenId,HttpServletResponse response){
        if(logger.isInfoEnabled()){
            logger.info("search for subscriber info; openId: "+subscriberOpenId);
        }
        HttpResult result = this.ssbService.findSubscriberInfo(subscriberOpenId);
        if(logger.isInfoEnabled()){
            logger.info("searching result: "+ result);
        }
            //JsonNode node = MAPPER.readTree(result.toString());
            //response.setHeader("Access-Control-Allow-Origin", "*");
        return result.getBody();
    }
}

result.getBody()的数据格式为:

{"id":"ojXQbxLbEY9wnrFgnL5ODCyVVpnc","userName":null,"phone":"15601905361","email":"kdnxjx@qq.com","emailPwd":"jdbxisbx","kindleEmail":"jdbxjx@kindle.com","subscribeDate":1466435475701}

页面会提示404错误:

angular-1.3.0.js:8452 GET http://localhost:8080/Weixin/getSubscriberInfo?subscriberOpenId=ojXQbxLbEY9wnrFgnL5ODCyVVpnc 404 (Not Found)

请问我应该如何正确返回这个页面并解析出数据呢?谢谢

PHP中文网
PHP中文网

认证高级PHP讲师

모든 응답(3)
阿神

수정 방법: @Controller 주석을 @RestController

로 수정합니다.
Ty80

특별한 구성이 없으면 result.getBody() 문자열의 JSP를 찾으러 가야 하는데 이는 분명히 원하는 결과가 아닙니다.
방법 이전에 구성해 보세요@ResponseBody

으아악

또는 @Controller를 @RestController로 변경하세요

小葫芦

angular는 ajax를 사용하며 이 메서드에는 @Responsbody가 필요합니다. 그렇지 않은 경우 반환된 결과를 기반으로 jsp를 검색할 수 있습니다

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿