1

I'm getting mad: I'm working with angular js and I don't know how to post data correctly.

I've filled some html input fields, whose values are stored in (ex.) $scope.prodotto, and I must send the json structure of $scope.product to a webservice I've written.

Unfortunately using the inspector, I' can't see anything.

I mean: I've written this:

$scope.salva = function() {
    console.log($scope.prodotto);

    $http.post('./service/prodotti/create', $scope.prodotto).error(function(data, status, headers, config){
        console.log($scope.prodotto);
    });

};

Salva is binded to a button. through ng-click argument. When I click the button, "salva" is triggered. I see that $scope.prodotto is populated with the right values (thanks to console.log), but...the server receives nothing, and the browser doesn't send anything (inspecting the req with chrome's inspector, shows an empty array)

So what am I doing wrong?

Thanks in advance!

2
  • Your angular looks right. What is with the . in your server path? Commented Feb 18, 2014 at 22:24
  • It's a relative path from the root of the app I'm writing, where the service is. and is right (I suppose) because all get requests works like a charm Commented Feb 18, 2014 at 22:28

2 Answers 2

1

I've found the solution.

Unfortunately the variable "$scope.prodotto" has been initialised as an array:

$scope.prodotto = [];

The solution is simple. Initialize it as an object:

$scope.prodotto = {};

That's it.

Sign up to request clarification or add additional context in comments.

Comments

0

You are only checking the ajax call if there is an error with your original post. you need to use the success callback like this:

$scope.salva = function() {
    console.log($scope.prodotto);

    $http.post('./service/prodotti/create', $scope.prodotto)
    .success(function(data, status, headers, config){
        console.log($scope.prodotto);
    });
    .error(function(data, status, headers, config){
        console.log($scope.prodotto);
    });

};

2 Comments

I know it bro, thank you, but the point is that inspecting the request from Chrome, my post request is EMPTY. this is the point. :)
Sorry this might be of help-> victorblog.com/2012/12/20/…

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.