没有从其他来电

时间:2018-01-08 07:18:10

标签: javascript ajax rest aurelia-http-client

我需要能够在我们的应用程序中为不同的流程区分400到500范围的错误代码。

假设我有3次休息,

  • 首先会返回200,
  • 秒将返回401,
  • 和第三个将返回502

我开始使用aurelia-http-client,这是我第一次注意到我在promise`s拒绝回调中收到http错误code = 0的地方。

更新:使用aurelia-fetch-client只返回一个字符串作为错误响应,因此也不是一个选项。

然后我尝试使用ajax call和基本XMLHttpRequest,这产生了相同的结果。对于200范围,我得到了代码,但是上面的任何内容,我都收到了一个0的statusCode。

更新:我正在运行Chrome的63.0.3239.132版本,如果它有所作为。

我尝试了什么:

  • 我尝试了5种不同的fetch变种。

    fetch(url, {
      method: requestMessage.method,
      headers,
      body: JSON.stringify(content)
    })
    .then((result) => {
      resolve(result)
    })
    .catch((error) => {
      reject(error); 
    });
    
  • 输出字符串错误

使用aurelia-http-client

    this.httpClient.createRequest(url)
      .asPut()
      .withContent(params)
      .send()
      .then((response) => {
        resolve(response);
      },
      (error) => {
        reject(error);
      });

- StatusCode始终为0表示错误

另外(这只是建立一个动态的XmlHttpRequest):

private retryRequest(): void {
  var xhr = this.setupXhr();
  xhr.onreadystatechange = () => this.stateChange(xhr);
  setTimeout(() => {
    xhr.send(JSON.stringify(this.content));
  }, 1000);
}

private setupXhr(): XMLHttpRequest {
  var xhr = new XMLHttpRequest();
  xhr.open(this.method, this.url, true);
  xhr = this.addHeaders(xhr);
  return xhr;
}

private addHeaders(xhr: XMLHttpRequest): XMLHttpRequest {
  for (let key in this.headers) {
    if (this.headers.hasOwnProperty(key)) {
      xhr.setRequestHeader(this.headers[key].key, this.headers[key].value);
    }
  }
  return xhr;
}

private stateChange(xhr: XMLHttpRequest): void {
  logger.debug(' ::>> xhr = ', xhr);
  if (xhr.readyState === 4) {
    if (xhr.status >= 200 && xhr.status < 400) {
      this.resolve(JSON.parse(xhr.response));
    } else if (xhr.status >= 500) {
      this.retryRequest();
    } else {
      // this.retryRequest();
      this.reject(xhr.response); // call after a # of fails for this ??? 
    }
  }
}
  • 仅返回200范围的http状态代码

此外:

    $.ajax({
      type: requestMessage.method,
      url,
      data: JSON.stringify(content),
      headers,
      success: (data) => {
        logger.debug(' ::>> rest call was a success ', data);
        resolve(data);
      },
      statusCode: {
        502: (jqXHR) => {
          logger.debug(' ::>> received 502 ');
          var retryAfter = jqXHR.getResponseHeader('Retry-After');
          retryAfter = parseInt(retryAfter, 10);
          if (!retryAfter) { retryAfter = 5 };
          setTimeout(query, retryAfter * 1000);
        }
      }
    });

- 永远不会得到502回调。我也尝试了其他状态代码

有没有办法获取我可能遗漏的错误代码?任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

我有类似的要求,并使用以下代码实现了这个

$.ajax({
        type: "POST",
        url: urlAjax,
        dataType: 'json',
        headers: headerValue,
        data: _dataValue,
        crossDomain: true,
        beforeSend: function () {
            //any operation
        },
        complete: function () {
            // any after operation
        },
        success: function (data) {
           // All 2XX will reach here 
        },
        error: function (jqXHR, textStatus, error) {
            var Check = JSON.parse(jqXHR['responseText']);
            // In above array you will get whole error response
        }
    }).done(function (rs, textStatus, xhr) {
        // on finished
    });

jqXHRtextstatuserrror参数中,您将收到有关错误和相关代码的所有信息。

希望有所帮助。

答案 1 :(得分:0)

发现问题:

我最初认为Harshal Bulsara是解决了我的问题的原因。这是一个很好的实现,但我发现了实际的问题。

我发现我们的nginx响应没有发送所需的标头。 通过添加Access-Control-Allow-Origin标头,我收到了statusCode。