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?

1
  • @RobertMoskal thanks for your comment. It needs JSON.stringify to fix key,value pair into raw data. Commented Mar 4, 2016 at 17:37

1 Answer 1

1

I finally found the answer.

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

The JSON.stringify is required.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.