knowbody
knowbody

Reputation: 8276

POST request in angular in django project with MultiValueDictKeyError

I'm getting this error while trying to POST some data through angular

$scope.voteProduct = function (user_id, company_id, action) {
    $http({
        method: 'POST',
        url: '/api/v1/user/company',
        params: {
            user_id: user_id,
            company_id: company_id,
            action: action
        }
    }).then(function (response) {
        return response.data;
    });
};

and my html file:

<button ng-click="voteProduct({{ user.id }}, [[ company.id ]], 'employ')" class="btn btn-primary" role="button">Up</button>

Upvotes: 1

Views: 426

Answers (1)

knowbody
knowbody

Reputation: 8276

After more reading I finally solved my problem, mostly based on this question

Here is the working version of my code:

    $http({
        url: '/api/v1/user/company',
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        data: $.param({
            user_id: user_id,
            company_id: parseInt(company_id),
            action: action
        })
    }).success(function (response) {
        return response.data
    });

The problem was: I didn't specify headers and also didn't include $.param in the data I was passing through.

Upvotes: 2

Related Questions