简体   繁体   中英

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.

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({
        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"}

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. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://CAN 'TSHOWTHIS.com' is therefore not allowed access.

Add below code in your web.config 's <system.webServer> section

<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({
        type: "POST",
        crossDomain: true,
        success: function (jsonResult) {
           //do what ever with the reply
        },
        error: function (jqXHR, textStatus) {
            //handle error
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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