2013-03-26 87 views
1

JS代碼爲什麼不工作 - jsonp和REST Easy?

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
    <script> 
    $.getJSON("http://localhost:8080/gbsshop/rest/auth/test/xyz?callback=?", function (data) { 
     alert("52"); 
    }); 
    </script> 
</head> 
</html> 

高枕無憂方法

@GET 
    @POST 
    @Path("/test/{param}") 
    @Produces({MediaType.APPLICATION_JSON }) 
    public String returnMessage(@PathParam("param") String msg) { 
     System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~"); 
     return "HEllo "+msg; 

    } 

我看到了從服務器獲取呼叫,但瀏覽器失敗,「未捕獲的SyntaxError:意外的標識符」

任何幫助表示讚賞。謝謝你的時間。

+2

,因爲它是一個jsonp請求,響應的格式應該是'({})',例如:'mycallback({msg:'my-message'})' – 2013-03-27 03:59:14

+0

回調方法的名稱可用作爲requestparameter'回調' – 2013-03-27 04:00:10

+0

真的嗎? - 我認爲「某人」在後臺照顧。我以爲你只需要從服務器發送json數據,而無需將其包裝在回調函數中。這可以讓我的代碼在服務器上看起來很難看。我誤解了你的評論,或者你的意思是我應該從我的returnMessage函數返回 - callbackName({result:「john」})?這不整潔嗎?有沒有更好的辦法。我正在嘗試你的建議。謝謝 – Mustafa 2013-03-27 04:04:45

回答

0

注意:這是在應用程序中執行它的最糟糕的方法,您需要檢查可能提供的框架支持。這只是演示如何添加JSONP支持

我不知道高枕無憂,我要在這裏採取

@GET 
@POST 
@Path("/test/{param}") 
@Produces({MediaType.APPLICATION_JSON }) 
public String returnMessage(@PathParam("param") String msg, @QueryParam("callback") String callback) { 
    System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~"); 
    return callback + "({msg: \"" + msg + "\"})"; 
} 

然後

$.getJSON("http://localhost:8080/gbsshop/rest/auth/test/xyz?callback=?", function (data) { 
    alert(data.msg); 
}); 

一大猜想在現實中你將不得不同時支持json和jsonp請求,因此您可能需要

@GET 
@POST 
@Path("/test/{param}") 
@Produces({MediaType.APPLICATION_JSON }) 
public String returnMessage(@PathParam("param") String msg, @QueryParam("callback") String callback) { 
    System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~"); 
    if (callback == null || callback == "") { 
     return "{msg: \"" + msg + "\"}"; 
    } else { 
     return callback + "({msg: \"" + msg + "\"})"; 
    } 
} 
+0

我想這個阿倫。但我會喜歡更清潔的服務器端代碼。還有什麼建議?謝謝。 – Mustafa 2013-03-27 04:14:59

+1

@阿倫我懷疑這會奏效。問題是框​​架可能會嘗試使用JSON庫像[Jackson](http://jackson.codehaus.org/)來轉換字符串結果。在上面提到的重複問題中有更好的解決方案 – Phil 2013-03-27 04:17:19

+1

菲爾我也在檢查。謝謝 – Mustafa 2013-03-27 04:19:44

2

Resteasy claims to support JSONP出3.x版盒子:

If you're using Jackson, Resteasy has JSONP that you can turn on by adding the provider org.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor (Jackson2JsonpInterceptor if you're using the Jackson2 provider) to your deployments. If the media type of the response is json and a callback query parameter is given, the response will be a javascript snippet with a method call of the method defined by the callback parameter. For example:

GET /resources/stuff?callback=processStuffResponse will produce this response:

processStuffResponse() This supports the default behavior of jQuery.

You can change the name of the callback parameter by setting the callbackQueryParameter property.

但是它似乎是博肯由於RESTEASY-1168: Jackson2JsonpInterceptor does not render closing bracket

所以 foo({"foo":"bar"} 呈現的 foo({"foo":"bar"})

來代替,而導致「未捕獲語法錯誤:意外的標識符「錯誤

我已模仿pull-request修復d希望它應該進入下一個版本3.0.12

我知道,這是qustion很老,但它顯示當你搜索RestEasy的JSONP問題谷歌的第一頁上,所以我決定更新