??yy 2016-06-08 14:44 采纳率: 0%
浏览 125

getResponseHeader为空

here is an Android mobile application , from which I'm making call to SAP SERVER .I came across a very strange problem. I'm making a ajax call to server and getting data as expected. but the problem is getResponseHeader is coming empty. But I can see the response header in browser console and it is as per my expectation. How to get the response header? Browser Console image

var a = {};
    a = {
        // object that contains HTTP headers as name value pairs 
        "Authorization" : "Basic " + btoa(username + ":" + password),
        "X-CSRF-Token" : "Fetch",        
    }, 
$.ajax({
        type: "GET",
        cache: false,
        url: requestUri1,
        headers: a,
        success: function(a, b, c) {
            globalTocken = c.getResponseHeader("X-CSRF-Token");
            alert(globalTocken);
        },
        statusCode: {
            401: function() {
                alert("User name and password is wrong");
            },
            403: function() {
                alert("error 403");
            }
        },
        error: function(a, b) {
            alert(b);
        }
    });

I have tried these ways also.

   OData.request    ({
             requestUri: requestUri1,
                   method: "GET",
                   headers:  {   
         "Authorization" : "Basic " + btoa(user_name + ":" + pass_word),
                                      "X-Requested-With": "XMLHttpRequest",
                                      "Content-Type": "application/atom+xml",
                                      "DataServiceVersion": "2.0",       
                                      "X-CSRF-Token":"Fetch"
                       }           
                },
                 function (data, response)
                 {
                      var header_xcsrf_token = response.headers['x-csrf-token'];
                      //console.log(header_xcsrf_token);
                      alert(header_xcsrf_token);

                 },function(err) {

                    //Error Callback:   

                    alert("Error occurred " + err.message  + err.response.statusText);

                });

Another way

var request = {
    headers : {
        // object that contains HTTP headers as name value pairs 
        "Authorization" : "Basic " + btoa(user_name + ":" + pass_word),
        "X-CSRF-Token" : "Fetch",        
    },
    requestUri : requestUri1, // OData endpoint URI 
    method : "GET",
    datatype : "json",
};
OData
        .read(
                request,
                function(data,response) {
                    x_csrf_token = response.headers["X-CSRF-Token"];
                }, function(err) {

                    //Error Callback:   

                    alert("Error occurred " + err.message  + err.response.statusText);

                });
    }
  • 写回答

1条回答 默认 最新

  • weixin_33699914 2016-06-20 09:48
    关注

    I have done lot of R&D and reached to a conclusion that all of the three coding are correct problem is from server side, it is not generating Token everytime so I have saved token in local memory and using it until the new one will not get generate from server(I'm making call to server everytime). it is working for me.

    function save_all(){ 
             var globalTocken,X_CSRF_Token,a = {};
    
        a.Authorization   = "Basic " + btoa("username" + ":" + "password"), 
        a["X-CSRF-Token"] = "fetch", 
        $.ajax({
            type: "get",
            cache: !1,
            url: requestUri1,
            headers: a,
            dataType: "xml",
            success: function(a, b, c) {
            if(!c.getResponseHeader("X-CSRF-Token")){
                    globalTocken = localStorage.savedTocken;
                    X_CSRF_Token = globalTocken;
            else{
                globalTocken = c.getResponseHeader("X-CSRF-Token");
                localStorage.removeItem("savedTocken");
                localStorage.setItem("savedTocken",globalTocken);   
                 X_CSRF_Token = globalTocken;
    
            }           
            },
            statusCode: {
                401: function() {
                    alert("User name and password is wrong");
                },
                403: function() {
                    alert("error 403");
                }
            },
            error: function(a, b) {
                alert(b);
            }
        }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题