0

I'm facing a very weird problem with a post request. I'm using Flask-Restless to create an API with GET and POST methods.

In my view a have a button and `onclick i would like to send a POST request.

My code:

var Insert = function(inputdata) {
    $.ajax({
        url: '/api/user',
        headers: {
            'Content-Type': 'application/json'
        },
        method: 'POST',
        dataType: 'json',
        data: {'description':'test'},
        success: function (data) {
            console.log('success: ' + data);
        }
    });
}

and

<button type="submit" class="btn btn-info" onclick="Insert()">Input</button>

When i'm trying to click the button i'm getting an error and specially :

POST http://127.0.0.1:5000/api/user 400 (BAD REQUEST)

a response

{"message": "Unable to decode data"}

If i try to send POST request through postman Chrome extension everything works fine.

Did i miss something?

4

1 回答 1

1

我终于找到了答案。

data: JSON.stringify({ 'description' : 'test'}),

JSON.stringify 是必需的。

于 2016-03-04T17:29:00.433 回答