[英]cross domain ajax POST to web-api always return empty response
I want to POST HTML form to web-api. 我想将HTML表单发布到web-api。
If I execute ajax POST with jQuery in other domain everything is OK and I receive 200 OK but in firebug and response tab I receive blank response like below image. 如果我在其他域中使用jQuery执行ajax POST,一切正常,我收到200, 但在firebug和response选项卡中,我收到空白响应,如下图所示。 this is my jQuery code:
这是我的jQuery代码:
var formData = new FormData($('form')[0]);
var response = '';
$.ajax({
url: 'http://localhost:2143/api/controller',
type: 'POST',
// Form data
data: formData,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false,
success : function(text)
{
response = text;
alert(response);
}
});
Please help me... 请帮我...
Due to the same origin policy restriction
that's built in browsers it is not allowed to perform cross domain AJAX calls. 由于浏览器内置的
same origin policy restriction
,因此不允许执行跨域AJAX调用。 You have a couple of possiblre workarounds: 您有几种可能的解决方法:
JSONP
- The idea here is that your server will wrap the JSON response in a callback. JSONP
这里的想法是您的服务器将JSON响应包装在回调中。 This works only with GET requests, so it might not be suitable for your case from what I can see you are attempting to send a POST request. JsonpMediaTypeFormatter
as shown in this similar post
. JsonpMediaTypeFormatter
,如this similar post
所示。 CORS
- the server should send a special Access-Control-Allow-Origin
response HTTP headers. CORS
服务器应发送特殊的Access-Control-Allow-Origin
响应HTTP标头。 The drawback here is that older browsers might not support it. blog post
covering this in more details. blog post
涵盖了更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.