javascript - Adding a custom header to HTTP request using angular.js -
i novice angular.js, , trying add headers request:
var config = {headers: { 'authorization': 'basic d2vudhdvcnrobwfuoknoyw5nzv9tzq==', 'accept': 'application/json;odata=verbose' } }; $http.get('https://www.example.com/applicationdata.svc/malls(1)/retailers', config).success(successcallback).error(errorcallback);
i've looked @ documentation, , seems me should correct.
when use local file url in $http.get
, see following http request on network tab in chrome:
get /app/data/offers.json http/1.1 host: www.example.com connection: keep-alive cache-control: max-age=0 if-none-match: "0f0abc9026855b5938797878a03e6889" authorization: basic y2hhzhn0b25lbwfuoknoyw5nzv9tzq== accept: application/json;odata=verbose x-requested-with: xmlhttprequest if-modified-since: sun, 24 mar 2013 15:58:55 gmt user-agent: mozilla/5.0 (x11; linux x86_64) applewebkit/537.22 (khtml, gecko) chrome/25.0.1364.172 safari/537.22 x-testing: testing referer: http://www.example.com/app/index.html accept-encoding: gzip,deflate,sdch accept-language: en-us,en;q=0.8 accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.3
as can see, both of headers added correctly. when change url 1 shown in $http.get
above (except using real address, not example.com), get:
options /applicationdata.svc/malls(1) http/1.1 host: www.datahost.net connection: keep-alive access-control-request-method: origin: http://mpon.site44.com user-agent: mozilla/5.0 (x11; linux x86_64) applewebkit/537.22 (khtml, gecko) chrome/25.0.1364.172 safari/537.22 access-control-request-headers: accept, origin, x-requested-with, authorization, x-testing accept: */* referer: http://mpon.site44.com/app/index.html accept-encoding: gzip,deflate,sdch accept-language: en-us,en;q=0.8 accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.3
the difference in code between these 2 1 first url local file, , second url remote server. if @ second request header, there no authentication header, , accept
appears using default instead of 1 specified. also, first line says options
instead of get
(although access-control-request-method
get
).
any idea wrong above code, or how additional headers included using when not using local file data source?
i took had, , added x-testing
header
var config = {headers: { 'authorization': 'basic d2vudhdvcnrobwfuoknoyw5nzv9tzq==', 'accept': 'application/json;odata=verbose', "x-testing" : "testing" } }; $http.get("/test", config);
and in chrome network tab, see them being sent.
get /test http/1.1 host: localhost:3000 connection: keep-alive accept: application/json;odata=verbose x-requested-with: xmlhttprequest user-agent: mozilla/5.0 (macintosh; intel mac os x 10_8_3) applewebkit/537.22 (khtml, gecko) chrome/25.0.1364.172 safari/537.22 authorization: basic d2vudhdvcnrobwfuoknoyw5nzv9tzq== x-testing: testing referer: http://localhost:3000/ accept-encoding: gzip,deflate,sdch accept-language: en-us,en;q=0.8 accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.3
are not seeing them browser, or on server? try browser tooling or debug proxy , see being sent out.
Comments
Post a Comment