Angular Parse GET Return - angularjs

m gets generated in a factory with the following request:
var m = $http({method: 'GET', url: JSONurl});
Console log of m after the GET request:
I need to grab m's "data:" which has the Array[2] I need. How would I create a new variable with just the data array?

If you look at the angularJS docs for $http, you'll see that you'll need to use the promise to get the data. So you want something along the lines of:
$http({
method: 'GET',
url: JSONurl
}).then(function successCallback(response) {
//response has the data on a successful call
}, function errorCallback(response) {
//this response will have the error data on a failed call
});

Related

Angularjs: Use the json file based on user selection

I have multiple json files as my datasource. How to use $http.get() to get the correct json files based on the user selection ?
try something like this
// Simple GET request example:
var fileName = 'myFile.txt' //name coming from where you call this service
$http({
method: 'GET',
url: '/someUrl/'+fileName
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
the alternative if you are using an api
var fileName = 'myFile.txt'
$http({
url: url: '/someUrl',
method: "GET",
params: {fileName: fileName}
});

how to receive array data in ajax request page wordpress

I am sending array to PHP through angularJs $http, but when I receiving data it was showing null, I don't know is that my procedure is correct or not,
js file is
var newdisable_comments_on_post_types = JSON.stringify(allTabData.disable_comments_on_post_types);
$http({
method:'post',
url:url,
params:{
'disable_comments_on_post_types': newdisable_comments_on_post_types
}
});
while sending in the header it sending like this
disable_comments_on_post_types:{"post":false,"page":false,"attachment":false}
in the PHP file, i did some of the procedure to receive it
$a = $_POST['disable_comments_on_post_types']['post'];// method 1
$a = $_POST['disable_comments_on_post_types'] // method 2
$x=1
foreach($a as $val){
$b[$x]=$val;
$x++;
}
$a = $_POST['disable_comments_on_post_types']->post;// method 3
I am getting null in response every method while I returning data to check
echo json_encode($a);
am I doing any wrong or in WordPress we cant send an array to PHP?
Change Your $http service to this:
By default, the $http service will transform the outgoing request by
serializing the data as JSON and then posting it with the content-
type, "application/json"
$http({
method: 'POST',
url: url,
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("&");
},
data: allTabData.disable_comments_on_post_types
}).success(function () {});

How to send an object array using $http post in angular? http error code 500

I'm new to angular and I'm trying to send an array with two objects using $ http post ...
I already tried to convert the array using angular.toJson,
I also tried to convert using JSON.stringify, I even tried using the two options and nothing works ... all my attempts result in an error of http code 500.
I do not know what to do anymore
I have the object 'ordemServico' that only has simple data all as string and when I only pass it in the post everything works:
$http({
method: metodo,
url: urlBase + 'ordensServico/',
data: self.ordemServico
}).then(function sucessCallback(response) {
self.atualizarTabela();
}, function errorCallback(response) {
self.ocorreuErro();
});
however, I need to send this object and a second object containing some files.
I'm trying to test just by passing the same object twice in an array, but it does not work at all:
$scope.final = new Object();
$scope.final={'ordemServico': self.ordemServico, 'imagens': self.ordemServico};
$http({
method: metodo,
url: urlBase + 'ordensServico/',
data: $scope.final
}).then(function sucessCallback(response) {
self.atualizarTabela();
}, function errorCallback(response) {
self.ocorreuErro();
});

Impossible to pass scope in http as data

I want to pass my $scope.data as data in $http-request.
$scope.data is not empty!
$http({
method: 'PUT',
url: url,
data: $scope.data
})...
But when sending this request the data is empty.
Try this shortcut method
From Angular docs
Look under the Shortcut methods section here
$http.put(url, data, config)
.then(
function(response){
// success callback
},
function(response){
// failure callback
});
If you are using node.js make sure you have included body-parser module and access it by request.body

Update $scope.variable value after POST

I am creating an simple TODO app using AngularJS, i POST the data to server when response comes, that response i want to store it existing variable and refresh the view. i.e
// This stores on page load, its working fine
var todos = $scope.todos = sever_passed_data;
but when i do,
$scope.$watch('todos', function () {
var request = $http({
method: "post",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
url: my_url,
data: $.param({todos_list: angular.toJson(todos)})
});
request.success(function(responce){
var todos = $scope.todos = responce;
});
}, true);
after this it gives me weird(it goes in infinite loop and posting data to server) output, i mean the responce doesn't stores in todos variable.
If you want to store the returned value of the HTTP POST in the $scope.todos variable, you should use response.data. response contains the entire HTTP response, including the response code, response headers, response body, etc.
Why are you declaring the local variable todos? It will go out of scope as soon as the function exits. just assign $scope.todos. Also, you might want to use then instead of success. Here's an example:
$http({
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
url: my_url,
data: $.param({todos_list: angular.toJson(todos)})
}).then(function(response) {
$scope.todos = response.data;
});

Resources