$http.get not working

31 views
Skip to first unread message

Yonatan Kra

unread,
May 15, 2014, 6:47:39 AM5/15/14
to ang...@googlegroups.com
Hi,
I have the following directive which sends a "get" request to the server.
Here is the code:
var params = {

    data: {
    'userID': $scope.tasks.id,
    'Age': $scope.aversion.choice,
    'Experience': $scope.aversion.choice,
    'Gender': $scope.aversion.choice,
    'Nationality': $scope.aversion.choice,
    'Assets': $scope.aversion.choice
    },
    task: 'questions'
   
    };
var httpConfig = {'params': params};
$http.get("ajax/dbHandler.php",httpConfig).success(function(data){
console.log(data);
});
This doesn't work. When I use the following address:
ajax/dbHandler.php?userID=5&Age=33&Experience=10&Gender=male&Nationality=somewhere&Assets=100
I get the correct response, but not with angular http.
What's the right way to use params?
thanks

Mickey Vashchinsky

unread,
May 15, 2014, 7:06:20 AM5/15/14
to ang...@googlegroups.com
Hi,

Documentation says that "params" and "data" should be defined on the config option, so you need:

var params = {
  'userID': $scope.tasks.id,
  'Age': $scope.aversion.choice,
  'Experience': $scope.aversion.choice,
    'Gender': $scope.aversion.choice,
    'Nationality': $scope.aversion.choice,
    'Assets': $scope.aversion.choice
};
var data = {
    task: 'questions'
};
var httpConfig = {
    'params': params, 
    'data': data
};

$http.get("ajax/dbHandler.php",httpConfig).success(function(data){
    console.log(data);
});

I didn't check it, but I think it should be ok.

Yonatan Kra

unread,
May 15, 2014, 7:22:53 AM5/15/14
to ang...@googlegroups.com
Hi,
Thanks for the answer.
Ok - now I need something a bit different - I want to send the whole JSON as a parameter so:
ajax/dbHandler.php?task=questions&data={userID:5,Age:33,Experience:10,Gender:male,Nationality:somewhere,Assets=100
I already did it in a different page like this:
ajax/dbHandler.php?task=login&coupon=123432&data={"time":"12/5/2014 00:00:00"}
but for some reason, it doesn't work here...


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/4EB4hSw64iY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Yonatan Kra

unread,
May 15, 2014, 7:44:49 AM5/15/14
to ang...@googlegroups.com
Ok, finally solved.
The magic trick of JSON.stringify should have been in the correct place:

var params = {

     data: JSON.stringify({
     'userID': $scope.tasks.id,
     'Age': $scope.aversion.choice,
     'Experience': $scope.aversion.choice,
     'Gender': $scope.aversion.choice,
     'Nationality': $scope.aversion.choice,
     'Assets': $scope.aversion.choice
     }),
     task: 'questions'
    
     };
var httpConfig = {'params': params};
$http.get("ajax/dbHandler.php",httpConfig).success(function(data){
console.log(data);
});
Now my DB updates correctly.
Thanks :)

Filipe Monteiro

unread,
May 15, 2014, 8:14:06 AM5/15/14
to ang...@googlegroups.com
Hi Yonatan,

I know you already solved the problem, but your first problem was: You don't always need to use the "data" attribute to specify params. According to what your server was expecting your params should be:

var params = {

     'userID': $scope.tasks.id,
     'Age': $scope.aversion.choice,
     'Experience': $scope.aversion.choice,
     'Gender': $scope.aversion.choice,
     'Nationality': $scope.aversion.choice,
    'Assets': $scope.aversion.choice,
     task: 'questions'
    
     };

Reply all
Reply to author
Forward
0 new messages