jQuery POST不发送JSON数据

raf*_*ale 2 rest wcf jquery post

我正在尝试使用jQuery AJAX对在localhost上运行的服务执行POST,但即使在我设置之后它也会继续返回状态代码0 jQuery.support.cors = true.我还可以从浏览器成功导航到我的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>
Run Code Online (Sandbox Code Playgroud)

有谁知道是什么原因引起的?我还要提一下,我不能使用jQuery在localhost上发布任何内容.

据Fiddler说,没有发送JSON数据,而是完成HTTP OPTIONS而不是POST.

小智 5

试试这个

var dataObj = {test:"test"};

var json = JSON.stringify(dataObj);
Run Code Online (Sandbox Code Playgroud)

然后在你的ajax电话中

data: json,
Run Code Online (Sandbox Code Playgroud)