[Flask] Possibly OT: result of form submission with Ajax is page's own sourcecode

Alejo Arias alejoar at gmail.com
Mon Apr 25 13:03:55 EDT 2016


Hi Alex,

You should post an example of your code, but it sounds like what's
happening is that you are using the same endpoint to return a json (when
the form validates correctly) and a render_template when the validation
fails. Then you submit something that doesn't validate and get the result
you are experiencing.

What I would do is create two endpoints instead, one that only renders the
template and the other that only processes the form and always returns json
that's readable by your js script.

Regards,
Alejo

On 25 April 2016 at 18:16, Alex Hall <ahall at autodist.com> wrote:

> Hello all,
> I'm sorry if this is off topic. I've never seen this before, and I'm
> pretty new to web development. Hopefully it turns out to be something Flask
> is doing.
>
> I have a page made from templates and hosted by Flask. The page includes a
> form, with a couple JQuery datepickers, input fields, hidden fields,
> dropdown, and a checkbox. My JS normally posts the form to itself, which my
> view function handles. That view function returns the rendered template if
> the form was sent via GET or failed validation, else it grabs the form
> contents, queries a database, and sends back some JSON for JS to parse.
>
> Today, as I was adding the datepickers and doing some other changes, my
> form suddenly started displaying an error in the web console that it
> couldn't parse the JSON. When I told it to print the JSON, I got back a
> string that is simply the page's own source code, given back to it. Flask's
> command line is showing no errors, and my view function is working
> properly. Somehow, though, the form is being handed HTML code of the page
> instead of JSON. The only error is when the JS tries to parse this, since
> it can't. Other than that, I'm seeing no problems anywhere--no syntax
> problems, nothing missing, nothing at all. I don't know why the result of
> an Ajax call in JS would be my page's source code, or if that means
> anything in particular. Hopefully I'm making sense, and am not too off
> topic. Thanks in advance for any thoughts.
>
> --
> Alex Hall
> Automatic Distributors, IT department
> ahall at autodist.com
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160425/347bb10f/attachment.html>


More information about the Flask mailing list