[Python] Can't get Web.py and jQuery to work out an AJAX GET request (it turns to an OPTIONS...

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    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.

    Here's the server-side 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()


    Here's the client-side code:

    <html>
    <head>
    <title>WTF</title>
    <script type="text/javascript" src="../static/jquery.js"></script>
    <script type="text/javascript">

    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();
    });

    </script>
    </head>
    <body>
    <div id="container"></div>
    </body>
    </html>


    This is Firebug's output:


    Fire in the hole! index.html (line 9) DONE.
    index.html (line 24) [ajax] Connection successful! ""
    index.html (line 17)
    ​

    Notice that the "" indicate that the request got empty data.

    This is what Firebug's network panel shows:

    [​IMG]

    If i open the page that Firebug indicates the data's there alright but if I, quite simply open http://0.0.0.0:8080/ on any browser, the data is displayed as expected! What is happening here?

    Finally, here's Web.py's log:

    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


    I'm coding in Ubuntu 12.04 LTS by the way.

    PS: I also tried changing the response header inside Web.py to:

    web.header('Content-Type', 'text/plain')


    but it didn't work.

    PS2: Changing the server address on the client-side script to "127.0.0.1:8080" or "localhost:8080" didn't help either.

    Continue reading...

Compartilhe esta Página