2016-12-08 11 views
0

目標は、ユーザーのストアド検索を削除するajax呼び出しを行うことです。200のステータスコードのエラーを返すASP.NET WEB APIへの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)); 
     } 
    }); 

私は次のエラーを取得する:

error:{"readyState":0,"responseText":"","status":0,"statusText":"error"}

いくつかの助けがapreciatedされるだろう。

EDIT:

呼び出しがクロスドメインである、と私は、次のメッセージが出ます:

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.

+2

これは相互ドメインのリクエストですか?コンソールをチェックして正確なエラーを見つけてください –

+0

あなたはそれが '200'であることをどのように知っていますか? –

+0

これはドメイン間の要求です。私はリクエストのステータスコードをチェックしたので、200です。 –

答えて

-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を有効にしますプロパティは$.ajaxtrue

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

私は '' * ''を' 'add name =" Access-Control-Allow-Origin "value =" * "/>'から適切な起源で置き換えました。それは魅力のように働いた。ありがとうございました! –

関連する問題