Java 阅读json web服务spring mvc
我有一个spring mvc Web服务,如下所示:Java 阅读json web服务spring mvc,java,json,web-services,jquery-mobile,spring-mvc,Java,Json,Web Services,Jquery Mobile,Spring Mvc,我有一个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@sa
@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调用是:
function hellowsfunction() {
$.ajax({
type: "GET",
url:"http://localhost:8080/ehCS-ui/rest/hello/getUser",
dataType: "json",
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);
}
});
}
web服务还可以,但Jquery似乎没有读取json obectj:
这是web服务在bowser上返回的用户对象
{"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}
请帮帮我。非常感谢看起来您需要更改:
$('#lblResult').append('<p> deleted: ' + msg.setDeleted+ '</p>');
$(''lblResult')。追加('已删除:'+msg.setDeleted+'');
到
$(''lblResult')。追加('删除:'+msg.deleted+'');
直接在浏览器中加载时会发生什么。服务显示json用户:{“版本”:null,“删除”:true,“插入日期”:null,“更新日期”:null,“所有者”:null,“用户ID”:null,“名称”:“样本名称”,“姓氏1”:null,“姓氏2”:null,“登录”:null,“学院编号”:null,“nif”:null,“电子邮件”:“样本”_email@sample.com,“需要姓氏2”:null,“telefonNumber”:null,“birthDate”:null,“inactive”:false,“inactiveReason”:null,“inactiveDate”:null,“position”:null,“professionals”:null,“applications”:null,“areals”:null,“sexType”:null,“locale”:null,“password”:null,“id”:null}对不起,我现在在q中看到了。您是否都在localhost上运行?是的,localhost中都是web服务响应“application/json”的响应mime类型?哦,是的,这是一个错误,但不能解决我的问题。回答是错误的#lblResult可以是一个div,也可以是一个- 列表?在执行任何
警报
消息之前,成功
或错误
函数中的某个地方似乎发生了错误。我建议使用Firebug或类似工具在控制台中调试获取输出的错误。我在chrome中使用javascript调试器进行了尝试,但控制台没有显示错误OCURR。它只显示statusText=error和responseText=“”,但是,如果我将数据更改为jsonp,则响应为Ok,状态为200,但jquery无法读取数据。
{"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}
$('#lblResult').append('<p> deleted: ' + msg.setDeleted+ '</p>');
$('#lblResult').append('<p> deleted: ' + msg.deleted+ '</p>');