我已经使用WebAPI2和MCV5开发了web应用程序。我正在尝试使用javascript调用Web并获取NetworkError:无法对“XMLHttpRequest”执行'send‘:无法将'Sub.domain.com’加载到colsole。
var param = {
"EmailID": email,
};
var appUrl = '@ConfigHelper.ApiURL';
var url = appUrl + 'user/checkemailid';
//create xml http request for request api url
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(param),
contentType: "application/json",
dataType: "json",
success: function (json) {
debugger;
if (json.IsSuccessful) {
swal("", "Email id already exist.", "warning");
return false;
}
else {
return true;
}
},
error: function (data) {
debugger;
swal('', data.Message, 'error');
}
});
并且还为项目web和api添加了交叉来源。将以下代码写入web.config
<system.webServer>
<!-- Cross origin for Domain-->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="POST,GET,PUT,PATCH,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
并将代码放入global.aspx文件中
protected void Application_BeginRequest()
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.Flush();
}
}
让我知道我遗漏了什么。
我正在尝试实现许多像Failed to execute 'send' on 'XMLHttpRequest' Syncronous这样的解决方案,但是没有成功
相同的代码片段适用于其他域的此项目,但不适用于主域到子域的api调用。
发布于 2018-07-04 19:04:45
您还可以在$.ajax()参数对象中添加以下内容-
"crossDomain": true,
"headers": {
"accept": "application/json",
"Access-Control-Allow-Origin":"*"
}
https://stackoverflow.com/questions/51074701
复制相似问题