简体   繁体   English

$ http标头不是函数-angularjs

[英]$http headers is not a function - angularjs

I am posting data to Dynamics CRM via SOAP on my PHP server with cURL . 我正在使用cURL在我的PHP服务器上通过SOAP数据发布到Dynamics CRM After this is done it is giving the entity GUID in the form of a HTTP Response header. 完成此操作后,它将以HTTP响应标头的形式提供实体GUID When attempting to access this via my angular factory and $http . 当试图通过我的角度工厂和$http访问它时。

My header is exposed and is able to be viewed in Chrome Developer tools and gives me the GUID I need. 我的标题已公开,可以在Chrome Developer工具中查看,并提供了我所需的GUID

The code for accessing the promise data is as follows: 访问承诺数据的代码如下:

        $http({
            method: 'POST',
            url: url,
            data: formData,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (data, headers) {

            var array = [];
            array.data = data;
            array.headers = headers('EntityId');
            console.log(array.headers);

            deferred.resolve(array);
        })

return deferred.promise;

//etc

The error I get is: 我得到的错误是:

headers is not a function() 标头不是function()

I can however, access some header result such as a status 200 code by using: 但是,我可以使用以下命令访问一些标头结果,例如状态200代码:

array.headers = headers;

But I need to access my custom header. 但是我需要访问我的自定义标头。 Any ideas on how I can achieve this? 关于如何实现此目标的任何想法?

As per Deprecation Notice on https://docs.angularjs.org/api/ng/service/$http 根据https://docs.angularjs.org/api/ng/service/$http上的弃用通知

The $http legacy promise methods success and error have been deprecated. $ http旧式的Promise方法成功和错误已被弃用。 Use the standard then method instead. 请改用标准然后方法。 If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error. 如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将引发$ http / legacy错误。

the preferred way would be: 首选方式是:

$http.get('/someUrl')
.then(function(response){
    var array = [];
    array.data = response.data;
    array.headers = response.headers('EntityId');
});

As Andy said already, headers is the 3rd parameter of the success callback. 正如Andy已经说过的那样,标头是成功回调的第三个参数。 So you will have to do this:- 因此,您将必须执行以下操作:-

success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  })

I wasn't going to add this as an answer but doing this as I wanted to add that headers is indeed a function. 我不想将其添加为答案,但这样做是因为我想添加标头确实是一个功能。

In my project, I did the below and saw function logged out as type in console. 在我的项目中,我执行了以下操作,并看到功能作为控制台中的类型注销。 The function returns the value of the header item corresponding to the name passed, if no parameters are passed, returns an object containing all headers. 该函数返回与传递的名称相对应的标题项的值,如果未传递任何参数,则返回包含所有标题的对象。

login(user) {
    return this.$http.post(this.url, user)
        .success((data, status, headers, config) => {
            console.log(typeof headers, 'headers'); => prints function
            console.log(headers(), 'headers'); => if you don't pass anything, returns an object containing all headers.

            return response;
        });
}

Excerpt from the angular code. 摘自角度代码。

function headersGetter(headers) {
var headersObj;

return function(name) {
if (!headersObj) headersObj =  parseHeaders(headers);

if (name) {
  var value = headersObj[lowercase(name)];
  if (value === void 0) {
    value = null;
  }
  return value;
}

return headersObj;
};

You parameters for success are incorrect. 您的成功参数不正确。 headers is the third parameter. headers是第三个参数。

$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

Check "Usage" section in https://docs.angularjs.org/api/ng/service/$http for reference. 请参阅https://docs.angularjs.org/api/ng/service/$http中的 “用法”部分,以供参考。

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise. $ http服务是一个带有单个参数(配置对象)的函数,该参数用于生成HTTP请求并返回承诺。

The response object has these properties: 响应对象具有以下属性:

data – {string|Object} – The response body transformed with the transform functions. data – {string | Object} –使用转换函数转换的响应主体。

  • status – {number} – HTTP status code of the response. status – {number} –响应的HTTP状态代码。
  • headers –{function([headerName])} – Header getter function. 标头 – {function([headerName])} –标头获取函数。
  • config – {Object} – The configuration object that was used to generate the request. config – {Object} –用于生成请求的配置对象。
  • statusText – {string} – HTTP status text of the response. statusText – {string} –响应的HTTP状态文本。

Angular version == 1.3.5 , Suppose header value has been set "X-AUTH-TOKEN = ' eyJwYXNzd29yZCI6ImFkbWlu '" in Application Security class after authentication. 角度版本== 1.3.5 ,假设身份验证后在Application Security类中已将标头值设置为“ X-AUTH-TOKEN =' eyJwYXNzd29yZCI6ImFkbWlu '”。

$scope.postData = "{\"username\" : username , \"password\": password ,\"email\" :email}";

$http({
            method: 'POST',
            url: '/API/authenticate',
            data: postData,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "X-Login-Ajax-call": 'true'
            }
        })
        .then(function(response) {
            if (response.data == 'ok') {
                $cookies['X-AUTH-TOKEN']=response.headers('X-AUTH-TOKEN');
                // below put,put,putObject Cookies value is valid for Angular version >= 1.4
                // $cookies.putObject('X-AUTH-TOKEN',response.headers('X-AUTH-TOKEN'); 
                window.location.replace('/');
            }
            else {

                // Error Message...
            }
        });

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

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