Using params in ngResource post URL

411 Views Asked by At

I'm working with a FosRestBundle API, and I must pass params in an url post resource to perform an operation.

The post resource in FosrestBundle is something like this :

api/readQuestion/{contain_id}/{user_id}/{type_id}/Questions

So to to send data using that resource in my angularjs app. I created a service and doing this way :

angular.module("QuestionRest", ['ngResource'])

.factory("question",  function ( $resource) {


    var apiData = $resource(
        "/api", {},
        {
            "postQuestion" : { method: "POST", url: "/api/readQuestion/:containid/:userid/:typeid/Questions"}
        });

    return {

        postQuestion: function(newQuestion, contain_id, user_id, type_id) {
            apiData.postQuestion(newQuestion, {containid: contain_id, userid: user_id, typeid: type_id}, function() {
                console.log("Success !");
            }, function(error) {
                console.log("Error " + error.status + " when sending request : " + error.data);
            });

        }

    }
});

But it doesn't work at all. When I call postQuestion in my app controller like this :

question.postQuestion(newQuestion, 1 ,1 ,1 ), 

I got this error :

angular.js:12410 POST http://localhost:8000/api/readQuestion/Questions?description=%3Cp%3Efdfdf%3C%2Fp%3E&title=fdf%3F&line=1000&page=1000 404 (Not Found)

Where :

{
"title": "fdf?",
"description": "Cp...3Efdfdf...",
"line": "1000",
"page": "1000"
 }

is the newQuestion JSON object sent by my post request.

You know what I meant.

What is the best way to pass parameters in an angular app post URL ?

1

There are 1 best solutions below

0
On BEST ANSWER

The parameters and the data are reversed:

The action methods on the class object or instance object can be invoked with the following parameters:

  • non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
    postQuestion: function(newQuestion, contain_id, user_id, type_id) {
        //apiData.postQuestion(newQuestion, {containid: contain_id, userid: user_id, typeid: type_id}, function() {

        var params = {containid: contain_id, userid: user_id, typeid: type_id};
        apiData.postQuestion( params, newQuestion,  function() {
            console.log("Success !");
        }, function(error) {
            console.log("Error " + error.status + " when sending request : " + error.data);
        });

    }