$http post not working in Safari

2019-06-28 05:30发布

问题:

I am developing an AngularJS app and I've implemented a simple login API call. The API gives me a response in all browsers except in Safari.

My login API call looks like this

$http.post(config.login, {
        email: username,
        password: password
    })
    .then(function (response) {
            callback(response);
        },
        function (response) {
            callback(response);
        });

I get the following response in Safari.

I am using Angular v1.4.8 a Safari Version 9.0 (11601.1.56)

Any clue on whats going wrong?

回答1:

This makes me crazy. First thing, I download angular.js file, then search for "xhr.setRequestHead". All problems are here. I've tried to put some logs to see what was happening.

forEach(headers, function(value, key) {
    key = key.trim();// trim the key
    value = value.trim();// trim the value
    console.log("******************** key: " + key);
    console.log("******************** value: " + value);
    console.log('value: Basic vs ' + value+ ' equal: '+ (value==="Basic"));
    console.log('value.length: ' + value.length);
    if (isDefined(value)) {
         xhr.setRequestHeader(key, value);
    }
});

I found that angularjs automatically put header 'Authorization':'Basic' in. But the length of 'Basic' string is 6 not 5. So using key = key.trim() and value = value.trim() solves problem.

Let me know if this help.