简体   繁体   English

跨域时,JQuery ajax JSONP POST更改为GET

[英]JQuery ajax JSONP POST changed to GET when cross domain

I have a WCF Service that is expecting a POST. 我有一个期待POST的WCF服务。 Using Fiddler I discovered that in cross-domain situations, my POST request was getting changed to a GET which results in error 405 from server. 使用Fiddler我发现在跨域情况下,我的POST请求被更改为GET,导致服务器出错405。

$.ajax({
    type: "POST",
    url: "http://blah/blah.svc/Test",
    data: JSON.stringify("{ 'WebUserID': 4 }"),
    dataType: "jsonp",  // from server
    contentType: "application/json; charset=utf-8", // to server
    success: function (data, status, xhr) {
        alert("success--");
    }
});

Can anyone shed some light on this? 任何人都可以对此有所了解吗?

Thanks 谢谢

There's no POST and JSONP. 没有POST和JSONP。 JSONP works by creating a new script tag in the DOM which sends a GET request to the server. JSONP的工作原理是在DOM中创建一个新的脚本标记,用于向服务器发送GET请求。 You're giving jQuery.ajax two incompatible parameters (POST, jsonp), and jQuery is choosing one over the other. 你给jQuery.ajax两个不兼容的参数(POST,jsonp),而jQuery正在选择一个而不是另一个。

One update: you can use something like CORS (Cross-Origin Resource Sharing) to enable non-GET requests to cross-domain services. 一个更新:您可以使用类似CORS(跨源资源共享)的功能来启用跨域服务的非GET请求。 WCF doesn't support it out-of-the-box, but I wrote a post about implementing it in WCF at http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf.aspx . WCF不支持开箱即用,但我写了一篇关于在WCF中实现它的帖子, 网址http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors -support-in-wcf.aspx

It's converting it to GET because you no longer have a name/value pair after doing the JSON.stringify ; 它正在将它转换为GET,因为在执行JSON.stringify之后你不再拥有名称/值对; you just have a string. 你有一个字符串。 POST requires a name/value pair. POST需要名称/值对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM