从JSP返回JSONP而不是JSON [英] Returning JSONP instead of JSON from a JSP

查看:90
本文介绍了从JSP返回JSONP而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置响应类型时发现了此问题从jsp转换为json,但我需要将响应类型设置为jsonp以便进行跨域访问.还会是这样:

I found this question on setting the response type to json from a jsp but I'm in need of setting the response type to jsonp for cross-domain access. Would it still be this:

response.setContentType("application/javascript");

,然后将来自jsp的响应包装在callbackfunction( + content + )中,还是需要做更多的事情?

and just wrapping the response from the jsp in callbackfunction( + content + ) or is there something more that needs to be done?

推荐答案

我最近不得不这样做.在服务器端,我有类似的内容:

I recently had to do this. In the server side I had something like so:

string callbackName = queryMap['callback']; //jquery will pass in some name in our .getJSON call below
string jsonData = getJsonData();
string jsonp = callbackName + "(" + jsonData + ")";

response.SetContentType('application/javascript');
response.Send( jsonp );

在javascript中,就像这样:

And in the javascript it was something like so:

var url = getUrl() + "?callback=?";
$.getJSON(url,function(onSuccessData){ alert(onSuccessData); });

这篇关于从JSP返回JSONP而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆