javascript 正确的 JSONP 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19247448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
correct JSONP Response
提问by invad0r
I'm trying locally to get JSONP to give me a correct response and pass it into my callback function jsonp_callback. Using code from: How do I set up JSONP?
我正在本地尝试让 JSONP 给我一个正确的响应并将它传递给我的回调函数 jsonp_callback。使用以下代码:如何设置 JSONP?
header('content-type: application/json; charset=utf-8');
$data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo $_GET['jsonpCallback'] . '('.json_encode($data).')';
and
和
$.ajax({
url: 'jsonp-response.php',
dataType:'jsonp',
jsonp: 'jsonp_callback',
success: function (r){
console.log(r);
}
});
function jsonp_callback (r){
console.log('jsonp_callback:',r);
}
Sofar I'm getting a Response which looks like:
到目前为止,我收到的响应如下所示:
jQuery1102035954900085926056_1381230228656({"a":1,"b":2,"c":3,"d":4,"e":5})
Looking at the first answer from Testing a static jsonp responseI think I'm doing it correctly but I'm not sure why jQuery gives me a unique string.
查看来自测试静态 jsonp 响应的第一个答案,我认为我做对了,但我不确定为什么 jQuery 给我一个唯一的字符串。
How would I make my response look like this ?
我怎样才能让我的回复看起来像这样?
jsonp_callback({"a":1,"b":2,"c":3,"d":4,"e":5})
回答by Sanjay Kumar
Here is the snippets from my code.. If it solves your problems..
这是我的代码片段.. 如果它解决了您的问题..
Client Code :
客户代码:
Set jsonpCallBack : 'photos' and dataType:'jsonp'
设置 jsonpCallBack : 'photos' 和 dataType:'jsonp'
$('document').ready(function() {
var pm_url = 'http://localhost:8080/diztal/rest/login/test_cor?sessionKey=4324234';
$.ajax({
crossDomain: true,
url: pm_url,
type: 'GET',
dataType: 'jsonp',
jsonpCallback: 'photos'
});
});
function photos (data) {
alert(data);
$("#twitter_followers").html(data.responseCode);
};
Server Side Code (Using Rest Easy)
服务器端代码(使用 Rest Easy)
@Path("/test_cor")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String testCOR(@QueryParam("sessionKey") String sessionKey, @Context HttpServletRequest httpRequest) {
ResponseJSON<LoginResponse> resp = new ResponseJSON<LoginResponse>();
resp.setResponseCode(sessionKey);
resp.setResponseText("Wrong Passcode");
resp.setResponseTypeClass("Login");
Gson gson = new Gson();
return "photos("+gson.toJson(resp)+")"; // CHECK_THIS_LINE
}
回答by Darin Dimitrov
Like this:
像这样:
$.ajax({
url: 'jsonp-response.php',
dataType:'jsonp',
jsonp: 'jsonp_callback',
success: jsonp_callback
});
function jsonp_callback (r) {
console.log('jsonp_callback:',r);
}
In your code you never actually used the jsonp_callback
function that you defined. You simply wrote some anonymous success callback. jQuery noticed that you used an anonymous function and that's why it generated this random name so that it can invoke the anonymous callback.
在您的代码中,您从未实际使用过jsonp_callback
您定义的函数。您只是编写了一些匿名成功回调。jQuery 注意到您使用了一个匿名函数,这就是它生成这个随机名称的原因,以便它可以调用匿名回调。