我在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)
请问我应该如何正确返回这个页面并解析出数据呢?谢谢
Kaedah pengubahsuaian: Ubah suai anotasi @Controller kepada @RestController
Jika tiada konfigurasi khas, dia harus pergi untuk mencari JSP rentetan
result.getBody()
, tetapi ini jelas bukan hasil yang anda mahu lihat.Cuba konfigurasikan sebelum kaedah
@ResponseBody
Atau tukar @Controller kepada @RestController
angular menggunakan ajax, dan kaedah tersebut memerlukan @Responsbody. Jika tidak, anda boleh mencari jsp berdasarkan hasil yang dikembalikan