Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Does the server response contain JSON with padding?</p> <p>You see, there are two ways to receive data from different domain:</p> <ol> <li><p><a href="http://en.wikipedia.org/wiki/Cross-origin_resource_sharing" rel="nofollow">CORS</a> is the new one but requires a separate <code>Access-Control-Allow-Origin</code> header to be sent from the server. Browser support is also somewhat limited for it.</p></li> <li><p><a href="http://en.wikipedia.org/wiki/JSONP" rel="nofollow">JSONP</a> doesn't rely on a server header but instead requires it to send back JSON data with so-called <em>'padding'</em>:</p> <pre><code>callback('{"your": ["json", "data"}') </code></pre> <p>Your JavaScript library (jQuery in this case) is then wraps it into a new <code>script</code> tag tricking your browser to evaluate it. Inside this <code>callback</code> function jQuery parses the JSON data and sends it to your <code>fnCallback</code>. After that it removes the unnecessary <code>script</code> tag from the page. In general it's a very fragile solution but it works in practice. For example, if the server doesn't pad JSON data you'll end up with the following code inside a <code>script</code> tag:</p> <pre><code>'{"your": ["json", "data"}' </code></pre> <p>Notice that it's just a string and does nothing. So, check if the server sends the wrapped JSON and not the raw JSON. I've run into the same problem quite a few times before and that helped me.</p></li> </ol> <hr> <p><strong>EDIT</strong>: Since you posted your server-side code I assume that you can modify it. I've never played with Django but I'll try to give you hints:</p> <ol> <li><p>Inside the <code>request</code> that you receive there should be a parameter called <code>callback</code> - that's what jQuery sends to you and what you should use for padding. Put it somewhere:</p> <pre><code># Like I said I don't know the exact API but you get the idea callback = request.GET["callback"] </code></pre></li> <li><p>After that <em>pad</em> your response before sending it to client:</p> <pre><code>response = HttpResponse( # Put callback name before the actual JSON callback + "(" + simplejson.dumps(response_dict) + ");", mimetype='application/javascript') </code></pre></li> </ol> <p>This should work.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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