使用application/json的jQuery ajax POST请求
我想从jQuery向远程服务器发出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
$.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没错。我想我会自动添加。那不是真的