jsonp跨域仅适用于IE
编辑:起初我以为它根本不能跨域工作,现在我意识到它只能在 IE 中工作
我正在使用 jQuery 调用 Web 服务(ASP.NET .axmx
),并尝试使用 jsonp
以便我可以在不同的站点上调用它。目前它仅适用于 IE,但不适用于 Firefox、Chrome、Safari。此外,在 IE 中,会弹出一个对话框,警告“此页面正在访问不受其控制的信息...”
有什么想法吗?
这是代码:
$.ajax({
type: "POST",
url: "http://test/TestService.asmx/HelloWorld?jsonp=?",
dataType: "jsonp",
success: function(data) {
alert(data.prop1);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status + " " + textStatus + " " + errorThrown);
}
});
服务器代码是:
[ScriptService]
public class TestService : System.Web.Services.WebService{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void HelloWorld() {
string jsoncallback = HttpContext.Current.Request["jsonp"];
var response = string.Format("{0}({1});", jsoncallback, @"{'prop1' : '" + DateTime.Now.ToString() + "'}");
HttpContext.Current.Response.Write(response);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很高兴它现在可以工作了。
您尝试发送参数“jsonp”——您需要将其传递给 json 的“padding”部分——作为 GET 参数,即在 URL 字符串中。这是正确的做法。
但因为您已指定 POST,所以不会发生这种情况。实际上,因为您指定了 POST,所以服务器期望所有参数都位于 POST 数据中,而不是 GET 变量中,因此它不会检查 URL 来检索参数。
我认为 jQuery 对于如何进行 JSON 评估可能非常宽容/聪明,因此仍然可以在 IE 中工作,因为 (a) 如果服务器没有读取“jsonp”变量,我认为它会发送back
"({'prop1' : ''})"
,它仍然可以作为 JSON 进行计算,并且 (b) IE 对跨域没有相同的限制与其他浏览器一样的站点脚本(“同源”策略)。但我需要调试它才能确定。我建议将来使用 Firefox 中的 FireBug 来调试此类请求的情况,但主要的要点是,如果您将参数作为 URL 的一部分发送,请使用 GET,而不是 POST。
干杯,
马特
Glad it's working now.
You're trying to send the parameter, "jsonp" -- that you need to pass for the "padding" part of the json -- as a GET parameter, i.e. in the URL string. Which is the right thing to do.
But because you've specified POST, that's not happening. Effectively, because you're specifying POST, the server is expecting all the parameters to be in the POSTed data, not in GET variables, so it's not checking the URL to retrieve the parameter.
I think it's possible that jQuery is being quite forgiving/smart about how it's doing the JSON evaluation, and therefore still working in IE, because (a) if the server doesn't read the "jsonp" variable, I think it'll send back
"({'prop1' : '<today's date>'})"
, which is still evaluate-able as JSON, and (b) IE doesn't have the same restrictions on cross-site scripting ("same origin" policy) as the other browsers. But I'd need to debug it to be sure.I'd suggest using FireBug in Firefox to debug what's going on with this sort of request in the future, but the main take-away is that if you're sending parameters as part of the URL, use GET, not POST.
Cheers,
Matt
除非您指定
jsonp
和/或jsonpCallback
选项,jQuery 会自动为您生成函数名称,并添加一个查询参数,例如callback=jsonp1272468155143
代码>.这意味着您的应用程序需要使用该函数名称进行输出。您始终可以将
jsonpCallback
设置为test
,在这种情况下您的示例将起作用。Unless you specify the
jsonp
and/or thejsonpCallback
option, jQuery auto-generates the function name for you, and adds a query param likecallback=jsonp1272468155143
. Which means your application needs to output using that function name.You can always set
jsonpCallback
totest
, in which case your example would work.