Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用application/json的jQuery ajax POST请求_Ajax_Jquery - Fatal编程技术网

使用application/json的jQuery ajax POST请求

使用application/json的jQuery ajax POST请求,ajax,jquery,Ajax,Jquery,我想从jQuery向远程服务器发出POST请求。当我写这样的代码时 $.ajax({ type: 'POST', url: 'http://mysite:8080/orderService/order/send', crossDomain: true, data: JSON.stringify(orderSendRequest), dataTy

我想从jQuery向远程服务器发出POST请求。当我写这样的代码时

$.ajax({
             type: 'POST',
                url: 'http://mysite:8080/orderService/order/send',
                crossDomain: true,
                data: JSON.stringify(orderSendRequest),
                dataType: 'json',
                success: function(responseData, textStatus, jqXHR) {
                    var value = responseData.someKey;
                },
                error: function (responseData, textStatus, errorThrown) {
                    alert('POST failed.');
                }
        });
一切正常,但我希望ContextType为application/json,当我将这一行添加到代码中时,请求不起作用,我出现以下错误:

XMLHttpRequest cannot load http://mysite:8080/orderService/order/send. Origin null is not allowed by Access-Control-Allow-Origin.

$.ajax({
             type: 'POST',
                url: 'http://mysite:8080/orderService/order/send',
                crossDomain: true,
                data: JSON.stringify(orderSendRequest),
                dataType: 'json',
                contentType : 'application/json; charset=utf-8',
                success: function(responseData, textStatus, jqXHR) {
                    var value = responseData.someKey;
                },
                error: function (responseData, textStatus, errorThrown) {
                    alert('POST failed.');
                }
        });

我不相信json支持跨域。改为使用jsonp数据类型进行研究。

您是否了解了服务器端的非内容类型和内容类型之间的区别?我认为,在添加数据类型时,没有必要添加内容类型?@wkaha contentType是您在调用中发送的数据类型,dataType是回调等待的数据类型。@Bardo没错。我想我会自动添加。那不是真的