store response header value during angular http post - angularjs

I am making an angular http post to an API which like this
$http({
method: 'POST',
url: 'http://api/ClientEndpoint',
data: register,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
}).error(function (data, status, headers, config) {
alert(data);
});
which is returning the following response
But I am not able to store the response path in a variable. How can I do that?
It is cross domain scenario and this is the response being showed in chrome console
I want to access store the ClientEndpoint value

Did you forget the .success(function (data) { }) callback ? just like the .error() you put after the http() function
i hope this will solve your problem ;)

Use the headers variable in either your .succes(response, headers) or .error(response, headers).
E.g :
.success(function(data, status, headers, config) {
//Success Handling
});
.error(function(data, status, headers, config) {
//Error Handeling
});
This only works if you are on the same domain as the server, if it's cross domain, the server has to send the Access-Control-Expose-Headers for you to make this work.

Related

Angular is waiting anoher request to start

In my App I need to do a request to save some data (this request is little slow) but the user can do another request in same time, when he did this, angular was waiting for the first request (not assync). How Can I do two requests whithout waiting for other?
My slow service:
app.service('PedSaveService',["$http", "$q","BASEURL",
function ($http, $q,BASEURL) {
var self = this;
self.Save = function (pedidoData) {
var deferred = $q.defer();
$http({method: 'POST',async: true, url: BASEURL.REST_SAVE, headers: {'Content-Type': 'application/json;charset=utf-8'}, data : pedidoData})
.success(function (response, status, headers, config) {
deferred.resolve(response, status, headers, config);
})
.error(function (response, status, headers, config) {
deferred.reject(response, status, headers, config);
});
return deferred.promise;
}
self.Save = function(pedidoData) {
$http({
method: 'POST',
async: true,
url: BASEURL.REST_SAVE,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
data: pedidoData
}).then(function(res) {
return res;
}); }
Your code in service should be this.

Angular $http is not sending data

I know it has been solved here many times, but I'm still not able to get it working.
My js call is:
var data = { value: 7 };
$http.post('api/controller/method', data);
But in fiddler there is no Content-Type and no JSON data.
I want the Content-Type to be 'application/json' which should be default, right?
Thanks
var data = { value: 7 };
$http({
url: "api/controller/method",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param(data)
}).success(function(data, status, headers, config) {
//some code when success post
}).error(function(data, status, headers, config) {
//some code when error post
});
Try this:
var response = $http.post('api/controller/method', data);
response.success(function(data, status1, headers, config) {
//
}

Why variable is not decremented?

In Angular JS controller there is variable:
$scope.currentCount = {
'subscribers' : 0
}
Also in HTML template:
<span>{{currentCount.subscribers}}</span>
When I call method in this controller, this variable is not decremented in template, why?
$scope.saveSubscriber = function (type, name) {
$scope.saveSubObj.type = type;
$http({
url: "/subscribe",
method: "POST",
data: $.param($scope.saveSubObj),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
$scope.currentCount.subscribers--;
}).error(function(data, status, headers, config) {
//TODO
});
}
Look at success AJAX response:
$scope.currentCount.subscribers--;
Are you sure it fired the success method? I just checked and got the correct result in my local testing.
Could you please put $scope.currentCount.subscribers--; inside the $http error() method as well? Let's check then whether it decreases or not.

Angularjs request encoding issue

As part of the service response there is some content received in local language. The response from the server looks fine, but the moment it's received through
angular.js $http, the content in local language is distorted.
I've tried both the methods given below, but no luck. Can someone help please?
Method 1:
appService.config(function($httpProvider) {
$httpProvider.defaults.headers.get = { 'Content-Type' : 'application/json;charset=utf-8' };
});
$http.get(url).success(function(data, status, headers, config) {
console.log('data: ' + data);
}).error(function(data, status, headers, config) {
console.log('data: ' + data);
});
Method 2:
$http.get(url,{ transformRequest: angular.identity,
headers: {'Content-Type': 'application/json;charset=utf-8'}}
).success(function(data, status, headers, config) {
console.log('data: ' + data);
}).error(function(data, status, headers, config) {
console.log('data: ' + data);
});
Angular response content looks something like this �����

How can I use POST in $http to send an object to the server?

I am using the following:
var url = '/api/Test/Mark';
$http('POST', url, $scope.content.answers)
.success(function (data, status, headers, config) {
$scope.content = data.text;
})
.error(function (data, status, headers, config) {
alert("Error: No data returned from " + url);
});
This gives me an error when I try to call it:
TypeError: Cannot use 'in' operator to search for '3' in POST
at isArrayLike (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:183:81)
at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:225:16)
at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:329:7
at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:227:18)
at extend (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:327:3)
at $http (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:6256:7)
at Scope.$scope.markQuestion (http://127.0.0.1:81/Content/app/questions/controllers/content.js:39:13)
at elementFns (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:8564:19)
at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:15511:13
at Scope.$get.Scope.$eval (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:10034:28)
Can someone give me advice on what I may be doing wrong? Note $scope.content.answers is an array of data.
You are doing wrong with $http
Try This
var url = '/api/Test/Mark';
$http({
method: 'POST',
url: url
data: $scope.content.answers // this should be a object e.g { a : 'one', b: 'Two' }
})
.success(function (data, status, headers, config) {
$scope.content = data.text;
})
.error(function (data, status, headers, config) {
alert("Error: No data returned from " + url);
});

Resources