1

I am trying to Post a json to my flask server, but the post request always fails. This is how I am doing it, any idea what is am doing wrong?

jQuery.ajax({ url:'/someLink', method:'POST', data:JSON.stringify({"sampleKey":"Value"}), success:function(data){ console.log(data); }, }); 

And my flask server looks something like

@app.route('/someLink', methods=['POST']) def someFn(): sampleKey = str(request.form['sampleKey']) 

I believe there is some data serializing issue, as the following request works

jQuery.post('/someLink',{"sampleKey":"Value"},function(data){console.log(data)}); 
2
  • Any progress? Did my solution work for you? Commented May 23, 2013 at 14:52
  • Hey Benjamin, I am stuck with my class homework, havent got time to look at it yet. Shall let you know in a few hours. Sorry. Commented May 23, 2013 at 17:19

1 Answer 1

1

jQuery's .ajax accepts either a plain object, or a query string as parameters for data. It does not accept JSON encoded strings.

It sends the data as 'application/x-www-form-urlencoded; charset=UTF-8' by default, which means unless you explicitly override this like Ian suggested possible (and the server is expecting it) you should not pass a JSON encoded string.

You can resolve this easily by passing the object.

From the API

data

Type: PlainObject or String

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

Try

jQuery.ajax({ url:'/someLink', method:'POST', data:{"sampleKey":"Value"}, success:function(data){ console.log(data); } }); 
Sign up to request clarification or add additional context in comments.

7 Comments

It does accept JSON encoded strings - you can set the contentType option as "application/json". But I think the server has to know how to understand/parse it. Nonetheless, your answer seems right, because you're passing the same object data as the OP's $.post()
@Ian this is new to me (JSON strings as data:) and seems contradictory to what the API docs say, would you mind whipping up a fiddle showing this (jsfiddle does JSON at /echo/json)?
@Ian Nevermind. I got what you mean, just sending a string as the BODY of the POST request and telling the server the form is encoded using application/json instead of url-encoded. It's not a specific feature. Good point, I'll update the answer, thanks.
Yeah, I guess it's only when the contentType is overridden. So it's not a $.ajax() feature technically. I probably shouldn't have mentioned it, but it caught my eye, because I know I've seen people use it before (not me personally). I wasn't trying to say it should be used here or yours is wrong; just commenting on that one sentence you have
Hey guys, unfortunately the soln didnt work. So I created a switch catch and used jQuery.get and jQuery.post both.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.