0

I'm working with a PhP interface. I must interact with this interface to save some data. For instance, to save a Customer, the PhP service wants the data through a POST request in this form

  • customer[customer_name] = "name of the customer"
  • customer[customer_code] = "code of the customer"
  • customer[customer_is_visible] = "whether or not the customer is visible"

I tried to create an AngularJS service to do that:

angular.module("app").factory("SaveCustomer", ["$resource", 
    function($resource){
        return $resource("/services/customers/post/", 
                            {
                                customer[customer_name]:'@name',
                                customer[customer_code]:'@code',
                                customer[customer_is_visible]:'@visibility',
                                dtsave:'true'
                            },
                            {
                                save: {
                                       method:'POST'
                                }
                            });
}]);

The problem is about the parameters name. Angular doesn't like the square bracket and it says: "Expected : but found [ in customer[custom ..." Can I achieve this task or do I have to modify the PhP interface to interact with? (note that the interface has not been created by me). Thank you.

EDIT: I call the service this way:

SaveCustomer.save({name:customerName, code:customerCode, visibility:customerIsVisible});
2
  • 1
    Can you post the line/s where you call to the $resource object ? Commented Mar 17, 2014 at 14:30
  • 1
    I edited the question. Take a look and thank you in advance. Commented Mar 17, 2014 at 14:38

2 Answers 2

1

Try turning customer into an object:

angular.module("app").factory("SaveCustomer", ["$resource", 
    function($resource){
        return $resource("/services/customers/post/", 
            {
                customer: { 
                    customer_name: '@name',
                    customer_code:'@code',
                    customer_is_visible:'@visibility'
                },
                dtsave:'true'
            },
            {
                save: {
                    method:'POST'
                }
            }
        );
    }
]);
Sign up to request clarification or add additional context in comments.

6 Comments

I need the array notation. The PhP interface would like to receive exactly customer[customer_name]='value', customer[customer_code]='code', customer[customer_is_visible]='value' ecc ecc as parameters"
thats how PHP will receive it. your browser will send it the way you are expecting it @superpuccio
How can I call the save function? Is this correct? SaveCustomer.save({name:customerName, code:customerCode, visibility:customerIsVisible});
that looks right, should be the same as before @superpuccio
Mmh...it doesn't seem to work. Have you ever verified that PhP receives data in the right "array" form passing parameters in your way? I suppose the answer is "yes" :)
|
1

I think you don't need to define anything in default parameters in $resource object, this code should work (I think so)

angular.module("app").factory("SaveCustomer", ["$resource", 
    function($resource){
        return $resource("/services/customers/post/", 
            {
                dtsave:'true'
            },
            {
                save: {
                    method:'POST'
                }
            }
        );
    }
]);

And you can call it:

SaveCustomer.save({customer: {customer_name:customerName, customer_code:customerCode, customer_is_visible:customerIsVisible}});

1 Comment

i'd still keep them in there to declare what is required by the factory, otherwise a customer could not have one of these three fields, if not all of them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.