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

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

répondre à tous(3)
阿神

Méthode de modification : Modifier l'annotation @Controller en @RestController

Ty80

S'il n'y a pas de configuration particulière, il devrait aller chercher la JSP de la result.getBody() chaîne, mais ce n'est évidemment pas le résultat que vous souhaitez voir.
Essayez de configurer avant la méthode@ResponseBody

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

Ou remplacez @Controller par @RestController

小葫芦

angular utilise ajax et la méthode nécessite @Responsbody. Sinon, vous pouvez rechercher jsp en fonction des résultats renvoyés

.
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal