简体   繁体   English

Angular Ajax请求发送对象

[英]Angular Ajax request sending object

i am editing content in an object saved in my AngularJs scope. 我正在编辑保存在AngularJs范围内的对象中的内容。 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: $ scope.newquestion具有以下对象:

[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: 但是ajax请求只显示了一个请求有效负载:

Request Payloadview source 请求Payloadview源

  {EvaluationQuestion:[]}
    EvaluationQuestion: []

Can any one guess why the sent data is empty? 任何人都可以猜出为什么发送的数据是空的?

It seems your $scope.newquestion is an Array , not an Object. 看来你的$scope.newquestion是一个数组 ,而不是一个Object。

JSON doesn't support an Array data type with named keys in it. JSON不支持带有命名键的Array数据类型。 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. 如果$scope.newquestion不需要是一个数组,只需使用一个Object。

Instead of 代替

$scope.newquestion = [];

change it to: 将其更改为:

$scope.newquestion = {};

I think you are using a wrong JSON syntax in $scope.newquestion. 我认为你在$ scope.newquestion中使用了错误的JSON语法。 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] . 我js数组你不能[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. 希望有所帮助。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM