当前位置: 首页 > 知识库问答 >
问题:

无法从rest调用获取http错误代码

燕涵容
2023-03-14
    null

然后,我尝试使用Ajax调用和基本的XMLHttpRequest,结果相同。对于200范围,我得到了代码,但在上面,我收到的statusCode为0。

更新:我正在运行63.0.3239.132版的Chrome,如果它有什么不同的话。

>

  • 我已经尝试了大约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

    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 ??? 
        }
      }
    }
    
      null
        $.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);
            }
          }
        });
    
  • 共有1个答案

    司马德水
    2023-03-14

    我有类似的需求,并使用以下代码实现了这一点

    $.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
        });
    

    jqxhrtextstatuserrrorparams中,您将收到与错误和相关代码有关的所有信息。

    希望能有所帮助。

     类似资料:
    • 我找了几个小时,没有一个能解决我的问题。我检查了命令,结果如下 @ionic/CLI-Utils:1.19.2 ionic(ionic CLI):3.20.0 > 错误:配置根项目'Android'时出现问题。 无法解析配置“:classpath”的所有文件。无法解析org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2。所需:项目:>com.android.tool

    • 我对Camunda很陌生,我想编写一个小的角程序,使用REST引擎控制已部署的BPMN流程。 到目前为止还不错,所以我从Camunda官方站点下载了开源社区平台。我还能够通过在上初始化的start-camunda.bat和TomCat启动所有内容。我的流程也被部署,我能够通过邮递员发出Rest电话来控制流程和获取信息。 所以REST通过邮递员工作打电话! Angular.json 如果我执行,它将

    • 问题内容: 我在Java应用程序中使用该类,并且需要能够获取HTTP状态代码。如果使用它,则可以在其中看到HTTP状态代码。我还有其他功能可以获取int或String的HTTP状态代码吗? 谢谢一群! 问题答案: 使用,它返回一个包含状态码,协议版本和“原因”的对象。

    • 问题内容: 我正在使用位于此处的教程:http : //addyosmani.github.io/backbone-fundamentals/#create- a-simple-web-server 并添加了以下代码。 在启动服务器后,我收到一条错误消息,指出访问时的错误,我只是想知道是否有人对这个错误了解很多,因为Express和Node对我来说是新的? 问题答案: 我认为您缺少路线,您需要定义

    • 这可能看起来很愚蠢,但是我试图在Axios中请求失败时获取错误数据。 是否可以获取一个可能包含状态代码和内容的对象,而不是字符串?例如:

    • 我是js/handlebars的新手,我无法用VS代码显示来自我的home.hbs文件的图像。当我运行服务器时,我得到的是: 这是我的服务器代码: 我的“home.hbs”代码: get“错误特别声明 无法获取/public/img/logo.png 这是我所有的信息,任何帮助将非常感谢。