DanielB
DanielB

Reputation: 31

http.get with params send word query

This is my first question, excuse my english.

I'm working with angular and I'm using http.post for all (get and post info). And how this is wrong, I'd start to use http.get for request the record from Database.

I'm using this:

$http.get("empresa.php",{params:{id:-1,action:"get"}})

I expect receive something like this:

empresa.php?id=-1&action=get

But not work, because send this:

empresa.php?query=%7B%22id%22:-1,%22action%22:%22get%22%7D

Could you help me?

query is not so I'expect to be.

I'm using Angularjs 1.5.8

EDITION Te factory is this:

app.factory("Data", ['$http',
    function($http, ) { // This service connects to our REST API

        var serviceBase = 'service/';

        var obj = {};

        obj.get = function(q,p) {
            console.log(p);
            return $http.get(serviceBase + q + ".php",{params:p}).then(function (results) {
                return results.data;
            });
        };
        obj.post = function(q, object) {
            return $http.post(serviceBase + q + ".php", object).then(function(results) {
                return results.data;
            });
        };
        obj.put = function(q, object) {
            return $http.put(serviceBase + q + ".php", object).then(function(results) {
                return results.data;
            });
        };
        obj.delete = function(q) {
            return $http.delete(serviceBase + q + ".php").then(function(results) {
                return results.data;
            });
        };

        return obj;
    }
]);

for POST work fine, but for GET doesn't work :(

New Edit

Here you can see how is that I have the code, how use and the response

Edit - Finalized

At the end, I resolved using $httpParamSerializer to serialize the json object and put it directly on call to empresa.php

Thanks to all for the help!!!

Upvotes: 0

Views: 254

Answers (2)

Anthony C
Anthony C

Reputation: 2157

Use $httpParamSerializer to build the url query for you.

var params = {id:-1,action:"get"};
var query = $httpParamSerializer(params);
$http.get("empresa.php?" + query);

Upvotes: 1

Anthony
Anthony

Reputation: 1837

Try not using the $http.get shortcut method.

$http({
    url: "empresa.php", 
    method: "GET",
    params: {id:-1, action:"get"}
});

Or else you can create the url yourself.

$http.get('empresa.php?id=' + -1 + '&action=' + 'get');

Upvotes: 0

Related Questions