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

我在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讲师

reply all(3)
阿神

Modification method: Modify the annotation @Controller to @RestController

Ty80

If there is no special configuration, he should run to find itresult.getBody()这个字符串的JSP了,但是这显然不是你希望看到的结果。
试试在方法前配置@ResponseBody

private @ResponseBody String getSubscriberInfo(@RequestParam("subscriberOpenId")String subscriberOpenId,HttpServletResponse response)

Or change @Controller to @RestController

小葫芦

angular uses ajax, and the method requires @Responsbody. If not, you can search for jsp based on the returned results

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template