1

i am editing content in an object saved in my AngularJs scope. Once submited i execute the following function:

$scope.saveQuestion = function(){       
    var request = $http({
        method: "post",
        url: "/manager/evaluations/evaluations/manage_questions/537c6179-8ed8-49b4-ac6b-25715f550349", 
        data: {EvaluationQuestion: $scope.newquestion}
    });
}

$scope.newquestion has the following object:

[relevance: 3, evaluation_topic_id: 1, type: "text", action: "add", name: "asdfasdfsadfas"] 

But the ajax request on the is just showing a Request Payload with:

Request Payloadview source

  {EvaluationQuestion:[]}
    EvaluationQuestion: []

Can any one guess why the sent data is empty?

4
  • Is the $scopre a typo? Commented Aug 7, 2014 at 12:48
  • Try console.log($scope.newquestion) just before the $http(). I suspect that the $scope.newquestion is still an empty array at the time a request is made. Commented Aug 7, 2014 at 12:51
  • The data showing the $scope.newquestion just before the $http() is posted in the question and continues to return the same array: [relevance: 3, evaluation_topic_id: 1, type: "text", action: "add", name: "Test again with console.log() just above the http() request"…] The feeling i got, is that calling the $scope.newquestion inside the $http call is not working.... Commented Aug 7, 2014 at 12:56
  • Can you create a plunker or something so we can completely see everything? Also, try stepping through the code using chrome or firebug to really track what's going on. Commented Aug 7, 2014 at 13:31

3 Answers 3

2

It seems your $scope.newquestion is an Array, not an Object.

JSON doesn't support an Array data type with named keys in it. For example:

var foo = [];
foo['bar'] = 'One';
foo.baz = 'Two';

JSON.stringify(foo); // return "[]"

If the $scope.newquestion isn't required to be an Array, just use an Object.

Instead of

$scope.newquestion = [];

change it to:

$scope.newquestion = {};
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are using a wrong JSON syntax in $scope.newquestion. Here you are defining an object which is supposed to be enclosed in {} but you are enclosing it in [] which stands for an array. I js arrays you cannot have [name1: value1, name2: value2].

Try replacing the square brackets with curely brackets, something like this:

{relevance: 3, evaluation_topic_id: 1, type: "text", action: "add", name: "asdfasdfsadfas"}

Hope that helps.

Comments

0

Try this:

data: {EvaluationQuestion: angular.toJson($scope.newquestion)}

2 Comments

Did not work, the Request Payload is the following doing that: EvaluationQuestion: "[]"
Console.log($scope.newquestion):: [relevance: 3, evaluation_topic_id: 1, type: "text", action: "add", name: "asdfsad fsdaf sda"] ------ Console.log(angular.toJson($scope.newquestion)):: []

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.