【问题标题】:How to take the header values in jquery [duplicate]如何在jquery中获取标题值[重复]
【发布时间】:2017-05-22 01:53:29
【问题描述】:

我在这里调用了一个 API,我得到了成功响应,但他们返回了总计数标题部分,我不知道如何获取这些值。
看这里:

$.ajax({
  url: '//www.examples.com/api/get/',
  type: 'GET',
  contentType: 'application/json; charset=utf-8',
  success: function(result) {
    console.log(result);
  },
  error: function(errMsg) { 
    //console.log(errMsg); 
  }
});

【问题讨论】:

  • 试试Response标签。

标签: javascript jquery rest api


【解决方案1】:

试试这个:request.getResponseHeader('x-Total-Count') 成功回调

$.ajax({
  url: '//www.examples.com/api/get/',
  type: 'GET',
  contentType: 'application/json; charset=utf-8',
  success: function(data, textStatus, request){
        alert(request.getResponseHeader('x-Total-Count'));
   },
  error: function(errMsg) { 
    //console.log(errMsg); 
  }
});

【讨论】:

    【解决方案2】:

    成功回调有3个参数,可以点击here

    $.ajax({
      url: '//www.examples.com/api/get/',
      type: 'GET',
      contentType: 'application/json; charset=utf-8',
      success: function (result, textStatus, xhr) {
        console.log(xhr.getResponseHeader('X-Total-Count'));
      },
      error: function(errMsg) { 
        //console.log(errMsg); 
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 2013-07-15
      • 1970-01-01
      • 2017-06-07
      • 2014-01-29
      • 2017-02-08
      • 1970-01-01
      • 2018-04-17
      相关资源
      最近更新 更多