jQuery POST 不发送 JSON 数据

发布于 2024-12-05 04:04:25 字数 925 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(5

凉薄对峙 2024-12-12 04:04:25

尝试这个

var dataObj = {test:"test"};

var json = JSON.stringify(dataObj);

然后在你的ajax调用中

data: json,

try this

var dataObj = {test:"test"};

var json = JSON.stringify(dataObj);

then in your ajax call

data: json,
眼眸印温柔 2024-12-12 04:04:25

我不想再花时间在这个问题上,所以我求助于使用原始 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.

-柠檬树下少年和吉他 2024-12-12 04:04:25

您应该使用网络监视器等工具来查看浏览器是否向服务器询问允许的标头(使用 OPTIONS 标头请求),您可能需要在实际请求发送到之前在 OPTIONS 响应中提供正确的标头服务器(请参阅底部的文章)。

另外,您可以尝试将其添加到实际调用或 ajaxSetup 中,因为您需要告诉浏览器发送凭据并允许跨域调用(我知道其他人已经提到了“crossDomain”):

$.ajaxSetup({
  crossDomain: true,
  xhrFields: {
    withCredentials: true
  }
});

如果你也有时间.. 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'):

$.ajaxSetup({
  crossDomain: true,
  xhrFields: {
    withCredentials: true
  }
});

Have a read of this if you get time too.. https://developer.mozilla.org/en/http_access_control

嘴硬脾气大 2024-12-12 04:04:25

因此,当请求是跨域时,jQuery 无论如何都会将您的 post 请求作为 get 请求发送。
您是否正在访问 URL 中的“localhost”,但您的应用程序将请求发送到计算机的本地 IP 而不是 localhost?因为这在技术上是跨域的,这意味着您不会以预期的方式收到请求。

例如(刚刚在本地测试过)
访问我的本地站点:

http://localhost/test/

站点上的表单通过 $.post() 提交到我的本地 IP 地址而不是 localhost

<form action="http://10.0.0.17/test/" method="post">
   ....[form stuff...]
</form>

这是跨域请求

如果您当调用 $.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:

http://localhost/test/

A form on the site submits to my local ip address instead of localhost via $.post():

<form action="http://10.0.0.17/test/" method="post">
   ....[form stuff...]
</form>

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

七度光 2024-12-12 04:04:25

将数据作为对象文字而不是字符串发送

data: '{"test":"test"}',

data: {test:"test"},

Send the data as an Object literal instead of a string

data: '{"test":"test"}',

to

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