使用application / json的jQuery ajax POST请求

时间:2022-10-09 11:42:25

I want to make a POST request to the remote server from jQuery. When I write code like this

我想从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.');
                }
        });

Everything is ok, but I want ContextType to be application/json, and when I add this line to the code the request doesn't work and I have the following error:

一切都很好,但我希望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.');
                }
        });

1 个解决方案

#1


0  

I don't believe json supports crossDomain. Research using jsonp datatype instead.

我不相信json支持crossDomain。使用jsonp数据类型进行研究。

#1


0  

I don't believe json supports crossDomain. Research using jsonp datatype instead.

我不相信json支持crossDomain。使用jsonp数据类型进行研究。