2013-06-18 95 views
0

一個Spring MVC的Web服務,我有這樣的Spring MVC的Web服務:消費與jQuery

@Controller 
@RequestMapping("/hello") 
public class helloWs { 
    @RequestMapping(value= "/getObj", method = RequestMethod.GET) 
    public 
    @ResponseBody 
     User prueba(@RequestBody User user) { 
     user.setEmail("[email protected]"); 
     user.setName("sample_name"); 
     user.setDeleted(true); 
     return user; 
    } 
} 

jQuery的調用這個Web服務是一個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":"[email protected]","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: Unexpected token :

In line 1 of the response.

怎麼了?

謝謝。

回答

1

你要返回的是JSON,而不是JSONP。

JSONP是使用填充的JSON,也就是說,您在開頭處放置了一個字符串,並在其周圍放置了一對括號。例如:

//JSON 
{"name":"stackoverflow","id":5} 
//JSONP 
func({"name":"stackoverflow","id":5}); 

http://www.mattlunn.me.uk/blog/2011/10/json-vs-jsonp/