2017-10-14 47 views
2

我有反應+ redux客戶端和C#API。我目前正試圖在反應中實現刪除方法。Axios.Delete()沒有執行那麼既沒有catch塊

patients.js

deletePatient(patientId) { 
    debugger; 
    axios.delete(url + 'patients/' + patientId).then(res => 
    {  
     this.props.dispatch(deletePatientAction(res)); 
    }) 
    .catch(function(error) { 
     console.log(JSON.stringify(error, null, 2)); 
    }); 
} 

PatientController.cs

[HttpDelete] 
    public HttpResponseMessage Delete(int id) 
    { 
     patientTasks.Delete(id); 

     return Request.CreateResponse(HttpStatusCode.OK); 
    } 

,當我在鉻調試它,我可以看到,那麼,不執行catch塊。請求沒有被我的C#控制器捕獲。

當我用郵遞員嘗試相同的請求時,請求正常被捕獲到API中。

如果我把URL作爲一個硬編碼字符串 axios.delete('http://localhost:1467/v1/patients/11')

你能告訴我發生了什麼(不會發生),這甚至不工作?

注:get方法是否正確

+0

如果你用郵遞員運行相同的請求,它工作嗎? – Freez

+0

是的,我在我的問題中告訴過它。 – pandemic

+0

對不起,我讀得太快了......你在Chrome網絡督察中看到了什麼? – Freez

回答

1

工作有了巨大的感謝我的朋友我張貼的工作方案。我錯過了默認標題。郵差是通過插入默認標題,與axios我必須自己定義它。

deletePatient(patientId) { 
     var config = { 
      headers: { 
       'User-Agent':'', 
       'Accept':'', 
       'Host':'' 
      } 
     }; 

     axios.delete(url + 'patients/' + patientId, config).then((res) => 
     {  
      this.props.dispatch(deletePatientAction(res)); 
     }) 
     .catch(function(error) { 
      console.log(JSON.stringify(error, null, 2)); 
     });    
}