Handling of Failure Status in HTTP OPTIONS - Ajax Call

前提是你 提交于 2019-12-08 06:02:21

问题


I tried to find out the solution for one of the existing question Error Handling in HTTP Ajax Call using $fetch Javascript - I tried to reproduce the said scenario. I found the issue, that is the HTTP call triggers the HTTP OPTIONS method. Finally its not returning any HTTP Status code. I checked the Netwok, its shows an empty status code and the status signal is grayed

Refer Preflight Table Request: https://docs.microsoft.com/en-us/rest/api/storageservices/preflight-table-request

Kindly assist me how to handle the failure of HTTP OPTIONS

I tried the following code

$fetch("http://localhost:1000/SearchUsers?....")
.then(response => {
        if((response != undefined) && (response != null)) {
          if (response.status && (response.status === 200)) {
            alert('Super');
            return response.json();
          } else {
            alert('Hai');
            return '';
          } 
        } else {
          alert('Oooops');
          return '';
        }
      })
.catch(err => {
        const errStatus = err.response ? err.response.status : 500;
        if (errStatus === 404){
          alert("HTTP 404");
        } else {
          alert("Network or Internal Server Error");
        }
});

The above said code always executes the Catch block and it alerts the message "Network or Internal Server Error".

I tried with following Header information too, but it fails.

headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Headers': '*',
    'Access-Control-Allow-Methods': 'POST, GET, PUT'
  }

Kindly assist me how to get the appropriate Error Status Code, in current scenario the requesting server is offline.

来源:https://stackoverflow.com/questions/45182002/handling-of-failure-status-in-http-options-ajax-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!