Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get Web.py and jQuery to work out an AJAX GET request (it turns to an OPTIONS request instead)
    primarykey
    data
    text
    <p>I am trying to get (the latest) Web.py and AJAX to play nice with each other, but so far I haven't had much luck.</p> <p>Long story short, I am running both the server-side (Web.py) and the client side (Javascript) on my local development computer, but somehow all my AJAX GET requests are showing up as OPTION requests. From what I've read, this is typical is cases of cross domain requests, but since I'm running this on localhost I am not sure what's going on.</p> <p>Here's the server-side code:</p> <pre><code>import web import json def make_text(string): return string urls = ('/', 'mainScreen', '/update', 'update' ) app = web.application(urls, globals()) global content content = {'key1': 'value1', 'key2': 'value2'} def getPayload(): return content class mainScreen: def GET(self): web.header('Content-Type', 'application/json') web.header('Access-Control-Allow-Origin', '*') web.header('Access-Control-Allow-Credentials', 'true') return getPayload() def OPTIONS(self): web.header('Content-Type', 'application/json') web.header('Access-Control-Allow-Origin', '*') web.header('Access-Control-Allow-Credentials', 'true') return getPayload() class update: def POST(self): global content content = web.input(_method='post') return "DONE." if __name__ == '__main__': app.run() </code></pre> <p>Here's the client-side code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;WTF&lt;/title&gt; &lt;script type="text/javascript" src="../static/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function dial() { console.log("Fire in the hole!"); $.ajax({ url: 'http://0.0.0.0:8080', contentType: 'application/jsonp', timeout : 5000, cache: false, success: function (data) { console.log('[ajax] Connection successful! ' + JSON.stringify(data)); }, error:function (jqXHR, textStatus, errorThrown){ console.log(JSON.stringify(jqXHR) + ' ' + textStatus +' '+errorThrown ); } }); console.log("Done."); } $(function() { dial(); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is Firebug's output:</p> <blockquote> <p>Fire in the hole! index.html (line 9) DONE.<br> index.html (line 24) [ajax] Connection successful! ""<br> index.html (line 17)</p> </blockquote> <p>Notice that the "" indicate that the request got empty data.</p> <p>This is what Firebug's network panel shows:</p> <p><img src="https://i.stack.imgur.com/HyxJt.png" alt="Firebug&#39;s network panel, showing the request"></p> <p>If i open the page that Firebug indicates the data's there alright but if I, quite simply open <code>http://0.0.0.0:8080/</code> on any browser, the data is displayed as expected! What is happening here?</p> <p>Finally, here's Web.py's log:</p> <pre><code> hal@ubuntu:~/Desktop/tut$ python app.py http://0.0.0.0:8080/ 127.0.0.1:43796 - - [26/Jul/2013 11:14:59] "HTTP/1.1 OPTIONS /" - 200 OK </code></pre> <p>I'm coding in Ubuntu 12.04 LTS by the way.</p> <p>PS: I also tried changing the response header inside Web.py to:</p> <pre><code>web.header('Content-Type', 'text/plain') </code></pre> <p>but it didn't work.</p> <p>PS2: Changing the server address on the client-side script to "127.0.0.1:8080" or "localhost:8080" didn't help either.</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. 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