jQuery POST 不发送 JSON 数据
我正在尝试使用 jQuery AJAX 对本地主机上运行的服务执行 POST,但即使在我设置了 jQuery.support.cors = true
之后,它仍然返回状态代码 0。我还可以从浏览器成功导航到我的 WCF REST 服务。这就是我的 JavaScript 的样子:
<script>
jQuery.support.cors = true;
$(document).ready(function(){
$.ajax({
type: "POST",
url: "http://localhost:8000/Test",
data: '{"test":"test"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert('success');
},
error:function(x,e){
if(x.status==0){
alert('error 0');
}
}
});
});
</script>
有谁知道是什么原因造成的?我还应该提到,我无法使用 jQuery 在本地主机上发布任何内容。
根据 Fiddler 的说法,不发送 JSON 数据,而是执行 HTTP OPTIONS 而不是 POST。
I'm trying to do a POST to a service running on localhost with jQuery AJAX, but it keeps returning status code 0 even after I've set jQuery.support.cors = true
. I can also navigate to my WCF REST service successfully from my browser. This is what my JavaScript looks like:
<script>
jQuery.support.cors = true;
$(document).ready(function(){
$.ajax({
type: "POST",
url: "http://localhost:8000/Test",
data: '{"test":"test"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert('success');
},
error:function(x,e){
if(x.status==0){
alert('error 0');
}
}
});
});
</script>
Does anyone know what could be causing this? I should also mention that I can't POST to anything on localhost using jQuery.
According to Fiddler, the JSON data is not sent, and a HTTP OPTIONS is done instead of a POST.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这个
然后在你的ajax调用中
try this
then in your ajax call
我不想再花时间在这个问题上,所以我求助于使用原始 HTML 表单 POST,因为在我的情况下 JSON 的使用并不是必需的。
对于在原始帖子中概述的相同问题的其他人,请参阅此线程以获取解释和解决方案:将 JSON 数据从 JQuery 发送到 WCF REST 方法时出现问题
总而言之,如果您的服务需要响应跨域,则需要能够处理 HTTP OPTIONS 方法来电。
I didn't want to spend anymore time on this issue, so I resorted to using raw HTML form POST as the usage of JSON wasn't essential in my case.
For anyone else having the same issues outlined in the original post, see this thread for an explanation and a solution: Problem sending JSON data from JQuery to WCF REST method
To summarize, your service needs to be able to handle the HTTP OPTIONS method if it is expected to respond to cross domain calls.
您应该使用网络监视器等工具来查看浏览器是否向服务器询问允许的标头(使用 OPTIONS 标头请求),您可能需要在实际请求发送到之前在 OPTIONS 响应中提供正确的标头服务器(请参阅底部的文章)。
另外,您可以尝试将其添加到实际调用或 ajaxSetup 中,因为您需要告诉浏览器发送凭据并允许跨域调用(我知道其他人已经提到了“crossDomain”):
如果你也有时间.. https://developer.mozilla.org/en/http_access_control
You should use a tool like network monitor etc. to see if the browser is asking the server for the allowed headers (using the OPTIONS header request), you may need to supply the correct headers in an OPTIONS response before the actual request is sent to the server (see the article at the bottom).
Also, you could try adding this to the actual call or the ajaxSetup, as you will need to tell the browser to send credentials and allow the cross domain call (I know someone else already mentioned 'crossDomain'):
Have a read of this if you get time too.. https://developer.mozilla.org/en/http_access_control
因此,当请求是跨域时,jQuery 无论如何都会将您的 post 请求作为 get 请求发送。
您是否正在访问 URL 中的“localhost”,但您的应用程序将请求发送到计算机的本地 IP 而不是 localhost?因为这在技术上是跨域的,这意味着您不会以预期的方式收到请求。
例如(刚刚在本地测试过)
访问我的本地站点:
站点上的表单通过 $.post() 提交到我的本地 IP 地址而不是
localhost
:这是跨域请求
如果您当调用 $.post() 或 jquery 的 ajax() 调用设置为 post 时,它会自动将参数从帖子正文移动到查询字符串中。
如果您正在访问本地主机,请尝试通过 jquery post() 方法用作域的任何地址访问该站点,看看是否有帮助。
有关跨域策略的更多信息,请参阅:
http://en.wikipedia.org/wiki/Same_origin_policy
So, when the request is cross domain, jQuery will send your post request as a get request anyways.
Are you accessing "localhost" in the URL but then your application is sending the requests to the local IP of your machine instead of localhost? Because that's technically cross-domain, which means that you won't receive the request in the expected manner.
E.g. (just tested this locally)
Visiting my local site at:
A form on the site submits to my local ip address instead of
localhost
via $.post():This is a cross-domain request
If you're calling $.post() or jquery's ajax() call set to post, it automatically moves your parameters from the post body into the query string.
If you ARE accessing local host, try hitting the site via whatever address your jquery post() method is using as the domain and see if that helps.
See more on cross-domain policies:
http://en.wikipedia.org/wiki/Same_origin_policy
将数据作为对象文字而不是字符串发送
到
Send the data as an Object literal instead of a string
to