javascript - $http in Anjgularjs: Can someone explain the flow -
In a $ http call, whatever we can pass in the URL part, I have a server address, a user Name and password Can I pass all as a json object, or do we have any other parameter (like URL) for it? Someone can help me understand what is happening on a successful call.
Specifically, the code I am battling is:
app.factory ('myFactory', function () {var fact = {}; fact .getData = function (a) {$ http ({method: 'POST', url: 'http: //100.100.100.100: 8080 / xx / xx.xx'}); $ Http.success (task (answer) { A (answer)});}; reality; })
See the code, however I can not find data from the server, a There are no errors at the same time. I
xapp.factory ('login data', function ($ http, $ log) {var fact = {}; facts .getData = function (cred, Cb) {return $ http ({method: 'post', url: 'xxx.xxx.xxx.xxx: xxxx / xxxxxx', data : Cred}) .success (function (data, position, header, config) {cb (data);}) panic (function (data, position, header, config) {$ log.warn (status);}); }; Return fact;}); Xapp.controller ('mainController', function ($ radius, $ log, $ http, login data) {$ scope.user = {uesr: 'user', password: '123'}; LoginData.getData ($ scope.user, Function (data) {$ scope.reply = data;});}); In the console log, I get an 'undefined' if the URL URL is correct, do you have any problems?
As far as I understand, a parameter is a callback function Which is executed when the response from the server is received. It promises that $ q kills the purpose of providing the service; Additionally, the $ http service does not have its own .success callback Returns the promise object with .success and .error with callback. Here's how it should be done: app.factory ('myFactory', function) {var facts = {}; Fact.getData = function () {return $ http.get ('your / path / to / resources /');} return fact;}). The controller ('myCtrl', function ($ radius, myFactory) {myFactory.getData (). Then (function (feedback) {//response.data keeps your data from server}, function () {// it fen When error occurs}}}}; Some explanations: myFactory.getData () creates a new http request on the server and a promo object Which is a method . Then (successCallback, ErrorColubback) . After the request is complete, you can provide a callback for the promise that is executed. You can get confused with the code you mentioned. Then (success callback, error callback) and .success (callback) used in your example Provides a generic promise that code $ q in .then , but $ http service, a On returning the promise, the shortcut provides .success () and .error () , but in the end it is the same thing.
Comments
Post a Comment