getResponseHeader为空

时间:2016-06-08 14:44:59

标签: android ajax jquery-mobile odata sap

这是一个 Android移动应用程序,我正在打电话给 SAP SERVER 。我遇到了一个非常奇怪的问题。 我正在对服务器进行ajax调用并按预期获取数据。 但问题是getResponseHeader是空的。但我可以在浏览器控制台中看到响应头,这符合我的期望。 如何获得响应标头? 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);
        }
    });

我也试过这些方法。

   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);

                });

另一种方式

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 个答案:

答案 0 :(得分:0)

我已经完成了很多R& D并得出结论,所有三个编码都是正确的问题是来自服务器端,它不是每次都生成令牌所以我已经将令牌保存在本地内存中并使用它直到新的一个人不会从服务器生成(我每次都打电话给服务器)。它对我有用。

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