正确的JSONP响应 [英] correct JSONP Response
问题描述
我试图在当地获得JSONP给我一个正确答案,并将其传递到我的回调函数jsonp_callback。使用code来源:我如何设置JSONP ? P>
标题(内容类型:应用程序/ JSON;字符集= UTF-8);
$数据=阵列('A'=大于1,'B'=> 2,'C'=> 3,D=> 4,'E'=> 5);
回声$ _GET ['jsonpCallback']。 (.json_en code($的数据)。);
和
$。阿贾克斯({
网址:'JSONP-response.php,
数据类型:JSONP,
JSONP:jsonp_callback,
成功:函数(R){
的console.log(r)的;
}
});
功能jsonp_callback(r)的{
的console.log('jsonp_callback:',R);
}
SOFAR我收到一个响应,看起来像:
jQuery1102035954900085926056_1381230228656({A:1,B:2,C:3,D:4,E:5})
看着从的第一个答案测试静态JSONP响应我想我中号正确地这样做,但我不知道为什么jQuery的给了我一个独特的字符串。
我将如何让我的回应是这样的?
jsonp_callback({A:1,B:2,C:3,D:4,E:5})
下面是我的code中的片段。如果能解决你的问题。
客户端code:
设置jsonpCallBack:'照片'和数据类型:JSONP
$('文件')。就绪(函数(){
VAR pm_url ='?的http://本地主机:8080 / diztal / REST /登录/ test_cor sessionKey = 4324234';
$阿贾克斯({
跨域:真正的,
网址:pm_url,
键入:GET,
数据类型:JSONP,
jsonpCallback:照片
});
});
功能照片(数据){
警报(数据);
$(#twitter_followers)HTML(data.response code)。
};
服务器端code(使用REST易)
@Path(/ test_cor)
@得到
@Produces(MediaType.TEXT_PLAIN)
公共字符串testCOR(@QueryParam(sessionKey)字符串sessionKey,@Context HttpServletRequest的HTT prequest){
ResponseJSON< LoginResponse> RESP =新ResponseJSON< LoginResponse>();
resp.setResponse code(sessionKey);
resp.setResponseText(错误通code);
resp.setResponseTypeClass(登录);
GSON GSON =新GSON();
回报的照片(+ gson.toJson(RESP)+); // CHECK_THIS_LINE
}
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?
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 response I think I'm doing it correctly but I'm not sure why jQuery gives me a unique string.
How would I make my response look like this ?
jsonp_callback({"a":1,"b":2,"c":3,"d":4,"e":5})
Here is the snippets from my code.. If it solves your problems..
Client Code :
Set jsonpCallBack : 'photos' and 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)
@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
}
这篇关于正确的JSONP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!