I am trying to send some data from my AngularJS Project to a PHP Server where I am using PHP Slim but I have tried everything I know and nothing seems to work.
SERVER PHP SLIM
$app->get('/create',function() use ($app) {
$data = $app->request()->params();
response_json(200,"DB: User Created",$data);
});
It works if I type directly from the browser
http://localhost:8888/core/users/create?login=test&password=123&name=Test&[email protected]&phone=12313
But if I try to send from the app using $http or $resource
var obj = { name:"Hello",email:"[email protected]"};
$http.get('/core/users/create?',obj).success(function(data){console.log(data); });
I get an empty Array[0].
And if I try to use $resource I got an obj but not how I expected.
.factory('qServer',function($resource){
return $resource('/core/users/create?:data',{data: '@data'});
});
var obj = { name:"Hello",email:"[email protected]"};
var send = JSON.stringify(obj);
//console.log(lol);
qServer.get({data:send},function(data) { console.log(data) });
With this code I get an Object like that:
data: Object
{"name":"Hello","email":"hello@email_com"}: ""
Anyone could tell me what I am doing wrong?
The second argument of $http.get method is not GET params but a request config.
You want to use something like this (notice the params key in config argument and lack of ? at the end of URL):
var obj = { name:"Hello",email:"[email protected]"};
$http.get('/core/users/create', {params: obj})
.success(function(data){console.log(data); });
See also: Q: $http get parameters does not work and config argument docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With