简体   繁体   English

如何在AngularJS上设置$ http标头?

[英]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. 我正在尝试为$ http创建基本身份验证,并且为此我尝试设置标头,但我不能这样做。

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. 不要将headers对象包装在_condigHeader对象中。

 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. 而且我不得不在我的网络服务中更改函数以获取jsonp的回调。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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