Ajax调用ASP.NET WEB API返回200状态代码的错误

时间:2016-12-08 09:01:26

标签: c# jquery asp.net ajax asp.net-web-api2

目标是进行ajax调用,删除用户的存储搜索。

方法如下:

    [HttpPost]
    [ActionName("DeleteStoredSearch")]
    public HttpResponseMessage DeleteStoredSearch(StoredSearch request)
    {
        _service.StoredSearchDelete(request.StoredSearchId);

        return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
    }

ajax调用如下所示:

    $.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的”: “”, “状态”:0 “状态文本”: “错误”}

一些帮助会被贬低。

编辑:

这个电话是一个跨域,我收到以下消息:

  

XMLHttpRequest无法加载https://“CAN'TSHOWTHIS”/ FacetedSearchApi / DeleteStoredSearch。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原始http://CAN'TSHOWTHIS.com'访问。

1 个答案:

答案 0 :(得分:-1)

web.config <system.webServer>部分

中添加以下代码
<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>

并在crossdomain中启用$.ajax属性true 示例

$.ajax({
        type: "POST",
        crossDomain: true,
        success: function (jsonResult) {
           //do what ever with the reply
        },
        error: function (jqXHR, textStatus) {
            //handle error
        }
    });