简体   繁体   English

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

[英]Ajax call to ASP.NET WEB API returning error for 200 status code

The goal is to make a ajax call that deletes stored searches of a user. 目的是进行ajax调用,以删除存储的用户搜索。

The method looks like this: 该方法如下所示:

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

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

The ajax call looks like this: 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));
        }
    });

I get the following error: 我收到以下错误:

error:{"readyState":0,"responseText":"","status":0,"statusText":"error"} 错误:{“ readyState”:0,“ responseText”:“”,“状态”:0,“ statusText”:“错误”}

Some help would be apreciated. 一些帮助将不胜感激。

EDIT: 编辑:

The call is a crossdomain, and i get the following message: 呼叫是跨域的,我收到以下消息:

XMLHttpRequest cannot load https://"CAN'TSHOWTHIS"/FacetedSearchApi/DeleteStoredSearch. XMLHttpRequest无法加载https://“ CAN'TSHOWTHIS” / FacetedSearchApi / DeleteStoredSearch。 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin http://CAN 'TSHOWTHIS.com' is therefore not allowed access. 因此,不允许访问原始站点http:// CAN'TSHOWTHIS.com'。

Add below code in your web.config 's <system.webServer> section 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>

and enable crossdomain property in $.ajax to true example 并启用$.ajax crossdomain属性为true示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM