Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery ajax json get call delivered by Flask web framework - call fails
    primarykey
    data
    text
    <p>I am trying to have a simple jquery ajax call fetching json data. However, the call fails and I cannot understand very well why.</p> <p>The code is below. To me it seems all right.</p> <pre><code>$(document).ready(function() { function onDataReceived (data){ alert('test'); }; $("button").click(function(){ $.ajax({ url: "/test", type: "GET", dataType: "jsonp", success: onDataReceived }); }); }); </code></pre> <p>EDIT Flask use</p> <p>The problem here was that I was using Flask and returning a json object. However, returning a pure json creates a cross domain conflict once you try to do the ajax call with jquery. Thus, much easier if you output jsonp from the flask view. To do that, you can use the snippet below (from here: <a href="http://flask.pocoo.org/snippets/79/" rel="nofollow">http://flask.pocoo.org/snippets/79/</a>). For the javascript piece you can refer to the one above, I only changed dataType from json to jsonp.</p> <pre><code>import json from functools import wraps from flask import redirect, request, current_app def support_jsonp(f): """Wraps JSONified output for JSONP""" @wraps(f) def decorated_function(*args, **kwargs): callback = request.args.get('callback', False) if callback: content = str(callback) + '(' + str(f().data) + ')' return current_app.response_class(content, mimetype='application/json') else: return f(*args, **kwargs) return decorated_function # then in your view @default.route('/test', methods=['GET']) @support_jsonp def test(): return jsonify({"foo":"bar"}) </code></pre> <p>I am not sure if there is a way to use straight json from flask, there probably is. However, as jsonp is also safer, I think better going with this anyway.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload