简体   繁体   中英

How to set $http headers on AngularJS?

I'm trying create a Basic Authentication to $http and to do this I'm trying set headers but I can't do this.

How could I do this ?

trying this.

var app = angular.module("starter");

app.service("UserLoginAPI", function($http, AppConstants, Base64){
    this.doLogin = function(){
        var _url = AppConstants.webServiceUrl + "/users/testaWS.json";
        var _authdata = Base64.encode('admin' + ':' + 'admin');

        var _condigHeader = {
            headers: {
                'Authorization': 'Basic ' + _authdata,
                'Accept': 'application/json; charset=utf-8',
                'Content-Type': 'application/json; charset=utf-8'               
            }
        };

        return $http.post(_url, _condigHeader);         

    };
});

Exception

OPTIONS http://192.168.1.105/GuairaFoods/users/testaWS.json (anonymous function) @ ionic.bundle.js:19346sendReq @ ionic.bundle.js:19165serverRequest @ ionic.bundle.js:18877processQueue @ ionic.bundle.js:23399(anonymous function) @ ionic.bundle.js:23415Scope.$eval @ ionic.bundle.js:24678Scope.$digest @ ionic.bundle.js:24489Scope.$apply @ ionic.bundle.js:24783(anonymous function) @ ionic.bundle.js:57605eventHandler @ ionic.bundle.js:12103triggerMouseEvent @ ionic.bundle.js:2870tapClick @ ionic.bundle.js:2859tapMouseUp @ ionic.bundle.js:2932
(index):1 XMLHttpRequest cannot load http://192.168.1.105/GuairaFoods/users/testaWS.json. Response for preflight has invalid HTTP status code 401
VM455:3 XHR Loaded (testaWS.json - 401 Unauthorized - 330.49999992363155ms - 0B)

Don't wrap the headers object in the _condigHeader object.

 var headers = {
                'Authorization': 'Basic ' + _authdata,
                'Accept': 'application/json; charset=utf-8',
                'Content-Type': 'application/json; charset=utf-8'               
            }

        return $http.post(_url, headers); 

Solved the problem.

I did.

var app = angular.module("starter");

app.service("UserLoginAPI", function($http, AppConstants, Base64){
    this.doLogin = function(){
        var _url = AppConstants.webServiceUrl + "/users/testaWS.json?callback=JSON_CALLBACK";
        var _authdata = Base64.encode('admin' + ':' + 'admin');

        var _headers = {
                'Authorization': 'Basic ' + _authdata,
                'Accept': 'application/json; charset=utf-8',
                'Content-Type': 'application/json; charset=utf-8'               
            };

        return $http({
                method: 'jsonp',
                url: _url,
                responseType: "json",
                headers: _headers
                });
    };
});

And I had to change the function in my webservice to get callback of jsonp.

public function testaWS(){            
            $this->autoRender = false;             
            $retorno = array("data"=>"ok");
            return $_GET['callback'] . '('.json_encode($retorno).')';            
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM