C# 对ASP.NET WEB API的Ajax调用返回200状态代码错误
目标是进行ajax调用,删除存储的用户搜索 方法如下所示:C# 对ASP.NET WEB API的Ajax调用返回200状态代码错误,c#,jquery,asp.net,ajax,asp.net-web-api2,C#,Jquery,Asp.net,Ajax,Asp.net Web Api2,目标是进行ajax调用,删除存储的用户搜索 方法如下所示: [HttpPost] [ActionName("DeleteStoredSearch")] public HttpResponseMessage DeleteStoredSearch(StoredSearch request) { _service.StoredSearchDelete(request.StoredSearchId); return new HttpRes
[HttpPost]
[ActionName("DeleteStoredSearch")]
public HttpResponseMessage DeleteStoredSearch(StoredSearch request)
{
_service.StoredSearchDelete(request.StoredSearchId);
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
$.ajax({
type: "POST",
url: "https://" + domain + "/buyer/api/v1_0/FacetedSearchApi/DeleteStoredSearch",
data: { "StoredSearchId": search_id },
success: function (result)
{
console.log('Successfully called');
$this_element.closest('.listing').remove();
var thing = document.getElementById('searchcount').innerHTML;
var thenumber = thing.match(/\d+/)[0]
document.getElementById('searchcount').innerHTML = thing.replace(/\d+/, thenumber - 1);
},
error: function (exception) {
alert("error:" + JSON.stringify(exception));
console.log("error:" + JSON.stringify(exception));
}
});
ajax调用如下所示:
[HttpPost]
[ActionName("DeleteStoredSearch")]
public HttpResponseMessage DeleteStoredSearch(StoredSearch request)
{
_service.StoredSearchDelete(request.StoredSearchId);
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
$.ajax({
type: "POST",
url: "https://" + domain + "/buyer/api/v1_0/FacetedSearchApi/DeleteStoredSearch",
data: { "StoredSearchId": search_id },
success: function (result)
{
console.log('Successfully called');
$this_element.closest('.listing').remove();
var thing = document.getElementById('searchcount').innerHTML;
var thenumber = thing.match(/\d+/)[0]
document.getElementById('searchcount').innerHTML = thing.replace(/\d+/, thenumber - 1);
},
error: function (exception) {
alert("error:" + JSON.stringify(exception));
console.log("error:" + JSON.stringify(exception));
}
});
我得到以下错误:
错误:{“readyState”:0,“responseText”:“status”:0,“statusText”:“error”}
需要一些帮助
编辑:
该调用是跨域调用,我收到以下消息:
XMLHttpRequest无法加载https://“CAN'TSHOWTHIS”/FacetedSearchApi/DeleteStoredSearch。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“TSHOWTHIS.com”
在
web.config
的
部分添加以下代码
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
</customHeaders>
</httpProtocol>
这是跨域请求吗?检查控制台以查找确切的错误您如何知道它是
200
?它是一个跨域请求。它是200,因为我检查了请求的状态代码。可能是重复的,我用适当的来源替换了来自
的“*”
,它就像一个符咒。非常感谢。