Generates an XHR request. The $xhr service adds error handling then delegates all requests to
$browser.xhr()
.
$xhr(method, url[, post], callback);
method – {string} –
HTTP method to use. Valid values are: GET
, POST
, PUT
, DELETE
, and
JSON
. JSON
is a special case which causes a
JSONP cross domain request using script tag
insertion.
url – {string} –
Relative or absolute URL specifying the destination of the request. For
JSON
requests, url
should include JSON_CALLBACK
string to be replaced with a name of an
angular generated callback function.
post(optional) – {(string|Object)} –
Request content as either a string or an object to be stringified as JSON before sent to the server.
callback – {function(number, (string|Object))} –
A function to be called when the response is received. The callback will be called with:
angular.service.$xhr.error
service.<script> function FetchCntl($xhr) { var self = this; this.fetch = function() { self.clear(); $xhr(self.method, self.url, function(code, response) { self.code = code; self.response = response; }); }; this.clear = function() { self.code = null; self.response = null; }; } FetchCntl.$inject = ['$xhr']; </script> <div ng:controller="FetchCntl"> <select name="method"> <option>GET</option> <option>JSON</option> </select> <input type="text" name="url" value="index.html" size="80"/><br/> <button ng:click="fetch()">fetch</button> <button ng:click="clear()">clear</button> <a href="" ng:click="method='GET'; url='index.html'">sample</a> <a href="" ng:click="method='JSON'; url='https://www.googleapis.com/buzz/v1/activities/googlebuzz/@self?alt=json&callback=JSON_CALLBACK'">buzz</a> <pre>code={{code}}</pre> <pre>response={{response}}</pre> </div>