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

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);
            }
        }); 
    
    评论

报告相同问题?