Alan2
Alan2

Reputation: 24562

How can I use POST in $http to send an object to the server?

I am using the following:

    var url = '/api/Test/Mark';
        $http('POST', url, $scope.content.answers)
            .success(function (data, status, headers, config) {
                $scope.content = data.text;
            })
            .error(function (data, status, headers, config) {
                alert("Error: No data returned from " + url);
            });

This gives me an error when I try to call it:

TypeError: Cannot use 'in' operator to search for '3' in POST
    at isArrayLike (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:183:81)
    at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:225:16)
    at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:329:7
    at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:227:18)
    at extend (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:327:3)
    at $http (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:6256:7)
    at Scope.$scope.markQuestion (http://127.0.0.1:81/Content/app/questions/controllers/content.js:39:13)
    at elementFns (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:8564:19)
    at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:15511:13
    at Scope.$get.Scope.$eval (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:10034:28)

Can someone give me advice on what I may be doing wrong? Note $scope.content.answers is an array of data.

Upvotes: 0

Views: 1620

Answers (1)

Nitish Kumar
Nitish Kumar

Reputation: 4870

You are doing wrong with $http

Try This

  var url = '/api/Test/Mark';

         $http({
                method: 'POST',
                url: url
                data: $scope.content.answers // this should be a object e.g { a : 'one', b: 'Two' }
            })
            .success(function (data, status, headers, config) {
            $scope.content = data.text;
        })
        .error(function (data, status, headers, config) {
            alert("Error: No data returned from " + url);
        });

Upvotes: 2

Related Questions