Angular帖子中的对象创建错误

时间:2013-09-10 11:53:03

标签: javascript angularjs

我有一个AngularJS控制器,如下所示:

function MainController($scope, $http) {
    //code for HTTP Get
    $scope.SelectionChanged = function () {
        //some initialization code
        $http.post("/api/cinema", { film: $scope.film })
            .then(function (result) {
                //success
            }, function (result) {
                //failure
            });
    }
}

SelectionChanged是一个绑定到范围的函数,以使某些选择元素响应。

我可以在fiddler中看到传递的对象是:

{"film":["HO00000335"]}

但我期望的是:

{"film":"HO00000335"}

我该怎么做?

Eidt:

现在,当我在列表中进行选择时,我会在fiddler中获得以下内容:

POST http://localhost:18669/api/cinema HTTP/1.1
Host: localhost:18669
Connection: keep-alive
Content-Length: 23
Accept: application/json, text/plain, */*
Origin: http://localhost:18669
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:18669/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

{"film":["HO00000262"]}

1 个答案:

答案 0 :(得分:1)

看起来$scope.film是一个数组。

试试这个:

function MainController($scope, $http) {
    //code for HTTP Get
    $scope.SelectionChanged = function () {
        //some initialization code
        $http.post("/api/cinema", { film: $scope.film[0] }) // note the [0]
            .then(function (result) {
                //success
            }, function (result) {
                //failure
            });
    }
}