【问题标题】:Read json web service spring mvc读取json web service spring mvc
【发布时间】:2013-06-17 16:17:59
【问题描述】:

我有一个这样的 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 调用是:

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);
             }
     });

    }   

结果应该在这样的 div 中。

 <div id ="lblResult" style="color:blue;">result   here</div>

但是我的 javascript 控制台一直显示这个错误,我不知道是什么问题。

Error. [object Object]

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}

请帮助我。非常感谢

【问题讨论】:

  • 当你加载这个时会发生什么:localhost:8080/ehCS-ui/rest/hello/getUser 直接在你的浏览器中。
  • 服务显示json用户:: {"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}
  • 抱歉,我现在在 q 中看到了。你都在本地主机上运行吗?
  • 是的,都在本地主机中
  • Web服务响应的响应mime类型是“application/json”吗?

标签: java json web-services jquery-mobile spring-mvc


【解决方案1】:

看来你需要改变:

$('#lblResult').append('<p> deleted: ' + msg.setDeleted+ '</p>');

$('#lblResult').append('<p> deleted: ' + msg.deleted+ '</p>');

【讨论】:

  • 哦,是的,这是一个错误,但它不能解决我的问题。回复还是错的。 #lblResult 可以是 div 还是应该是
      列表?
  • 在执行任何alert 消息之前,successerror 函数中似乎发生了错误。我建议使用 Firebug 或类似的工具来调试在控制台中获取输出的错误。
  • 我已经在 chrome 中使用 javascript 调试器进行了尝试,但控制台没有显示出现错误。它只显示statusText=错误,并且responseText =“”,但是,如果我将数据更改为jsonp,则响应是Ok,Status 200,但jquery无法读取数据。
猜你喜欢
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多