librelist archives

« back to archive

request.form cannot parse jQuery.post?

request.form cannot parse jQuery.post?

From:
刘一新
Date:
2011-12-02 @ 04:24
Hi,

I use jQuery.post with 'json' datatype to post a request to a view
function, and find that request.form can not parse the data post by $.post
Example:

Client side:

$.post($SCRIPT_ROOT+'/_feed',{ gname:gname_string,

 check:$("#somecheckbox:checked").length,
                                                 frame:frame_integer},
           function(data){
                 alert(data.response);},
           'json'
);

Server side:

@app.route('/_feed",methods=['POST'])
def feed():
    gname = request.form['gname']      # correct
    check = request.form['check']        # "0" and "1" are strings not
integer or False and True
    frame_id = request.form['frame']     # An integer is expected, however,
a string is returned
    return jsonify(response=some_response)

Am I missing something that is obvious to you? I also want to transfer a
list, how can I do that?

Thanks!

-- 
*Yi-Xin Liu, PHD*
*Department of Macromolecular Science*
*Fudan University*
*Room 415, Yuejing Building *
*Handan Rd. 220, **Shanghai, China*
*Tel +86-021-65642863*
*Mobile +86-13916819745*
http://www.mendeley.com/profiles/yi-xin-liu/

Re: [flask] request.form cannot parse jQuery.post?

From:
Armin Ronacher
Date:
2011-12-02 @ 04:42
Hi,

If it's JSON data it's not in request.form, then it's in request.json.
This is determined by the transmitting mimetype.


Regards,
Armin

Re: [flask] request.form cannot parse jQuery.post?

From:
刘一新
Date:
2011-12-02 @ 05:12
I just check the request.json, but it's value is None.
Any idea?
How can I set the MIME type when using jQuery.post()?

Thanks

2011/12/2 Armin Ronacher <armin.ronacher@active-4.com>

> Hi,
>
> If it's JSON data it's not in request.form, then it's in request.json.
> This is determined by the transmitting mimetype.
>
>
> Regards,
> Armin
>



-- 
*Yi-Xin Liu, PHD*
*Department of Macromolecular Science*
*Fudan University*
*Room 415, Yuejing Building *
*Handan Rd. 220, **Shanghai, China*
*Tel +86-021-65642863*
*Mobile +86-13916819745*
http://www.mendeley.com/profiles/yi-xin-liu/

Re: [flask] request.form cannot parse jQuery.post?

From:
Armin Ronacher
Date:
2011-12-02 @ 05:25
Hi,

On 12/2/11 6:12 AM, 刘一新 wrote:
> I just check the request.json, but it's value is None.
> Any idea?
> How can I set the MIME type when using jQuery.post()?
The data you are transmitting in the example is *not* JSON btw.  It's
URL encoded, so it should be in request.form.  The 'json' parameter
controls what jQuery expects the server to send, not what jQuery sends
if I remember correctly.

However neither of that is related to Flask, that really is a question
for the jQuery guys :-)


Regards,
Armin

Re: [flask] request.form cannot parse jQuery.post?

From:
Zach Williams
Date:
2011-12-02 @ 05:33
Made a gist on Github that should show you to check the request:
https://gist.github.com/1421913

Open up your console to see the JSON object, and visit the "/test" page to
see the text.

Zach

On Thu, Dec 1, 2011 at 9:25 PM, Armin Ronacher
<armin.ronacher@active-4.com>wrote:

> Hi,
>
> On 12/2/11 6:12 AM, 刘一新 wrote:
> > I just check the request.json, but it's value is None.
> > Any idea?
> > How can I set the MIME type when using jQuery.post()?
> The data you are transmitting in the example is *not* JSON btw.  It's
> URL encoded, so it should be in request.form.  The 'json' parameter
> controls what jQuery expects the server to send, not what jQuery sends
> if I remember correctly.
>
> However neither of that is related to Flask, that really is a question
> for the jQuery guys :-)
>
>
> Regards,
> Armin
>