jsonp跨域仅适用于IE

发布于 2024-08-31 03:33:46 字数 1080 浏览 1 评论 0 原文

编辑:起初我以为它根本不能跨域工作,现在我意识到它只能在 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);
    }
}

EDIT: At first I thought it wasn't working cross domain at all, now I realize it only works in IE

I'm using jQuery to call a web service (ASP.NET .axmx), and trying to us jsonp so that I can call it across different sites. Right now it is working ONLY in IE, but not in Firefox, Chrome, Safari. Also, in IE, a dialog pops up warning "This page is accessing information that is not under its control..."
Any ideas?

Here is the code:

$.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);
    }
}); 

And the server code is:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑脸一如从前 2024-09-07 03:33:47

很高兴它现在可以工作了。

您尝试发送参数“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

感性 2024-09-07 03:33:47

除非您指定 jsonp 和/或 jsonpCallback 选项,jQuery 会自动为您生成函数名称,并添加一个查询参数,例如 callback=jsonp1272468155143代码>.这意味着您的应用程序需要使用该函数名称进行输出。

您始终可以将 jsonpCallback 设置为 test,在这种情况下您的示例将起作用。

Unless you specify the jsonp and/or the jsonpCallback option, jQuery auto-generates the function name for you, and adds a query param like callback=jsonp1272468155143. Which means your application needs to output using that function name.

You can always set jsonpCallback to test, in which case your example would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文