我有一个像这样的spring mvc Web服务:
@Controller
@RequestMapping("/hello")
public class helloWs {
@RequestMapping(value= "/getObj", method = RequestMethod.GET)
public
@ResponseBody
User prueba(@RequestBody User user) {
user.setEmail("sample_email@sample.com");
user.setName("sample_name");
user.setDeleted(true);
return user;
}
}
对此Web服务的jquery调用位于包含以下函数的html文件中:
function hellowsfunction() {
$.ajax({
type: "GET",
url:"http://localhost:8080/ehCS-ui/rest/hello/getUser",
dataType: "jsonp",
success: function(msg) {
$('#lblResult').html('<p> Name: ' + msg.name + '</p>');
$('#lblResult').append('<p>email : ' + msg.email+ '</p>');
$('#lblResult').append('<p> deleted: ' + msg.setDeleted+ '</p>');
alert('Success: ' + response);
},
error: function (e) {
$("#lblResult").removeClass("loading");
alert('failed:'+e);
console.log(e);
}
});
}
结果应该是这样的div。
<div id ="lblResult" style="color:blue;">result here</div>
Web服务没问题,但似乎Jquery没有读取json obectj:这是Web服务在浏览器上返回的用户对象。
{"version":null,"deleted":true,"insertDate":null,"updateDate":null,"owner":null,"userId":null,"name":"sample_name","surname1":null,"surname2":null,"login":null,"collegiateNumber":null,"nif":null,"email":"sample_email@sample.com","surname2Required":null,"telefonNumber":null,"birthDate":null,"inactive":false,"inactiveReason":null,"inactiveDate":null,"position":null,"professionals":null,"applications":null,"areas":null,"sexType":null,"locale":null,"password":null,"id":null}
但是我的javascript控制台一直向我显示这个错误,我不知道出了什么问题。
Uncaught SyntaxError:意外的令牌:
在回复的第1行。
怎么了?
感谢。
答案 0 :(得分:1)
你要归的是JSON,而不是JSONP。
JSONP是带填充的JSON,也就是说,你在开头放了一个字符串,在它周围放了一对括号。例如:
//JSON
{"name":"stackoverflow","id":5}
//JSONP
func({"name":"stackoverflow","id":5});