Pregunta

Linux bogon 3.9.5-301.fc19.i686,nginx1.4.2 + web.py0.37 + uwsgi2.01

I writed a html page which gets a user's input(a text string), and post it to backend.The backend just read the input string and send back to the page.

<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#ExtButton").click(function(){
        var postdata = document.getElementById("url_to_get").value;
        jQuery.ajax({
            type: 'POST',
            url: "http://127.0.0.1/TextPickup",
            data: postdata,
            success: function(data,status){alert("status:"+ status +",data:" +data)},
            dataType:'html'
        });
    });
}); 
</script>
<input type="text" name="url_to_get" id="url_to_get" style="width:310px">
<button id="ExtButton">send</button>

and the python script in backend:

import web
import os
urls = (
'/','Index',
'/TextPickup','TextPickup',
'/TextAnalysis', 'TextAnalysis'
)

class Index:
    ...

class TextPickup:
    def POST(self):
        dest_url = web.data()
        return dest_url + ".."

class TextAnalysis:
    ...

but when I put something and click the button,the data is empty like below: enter image description here

I have checked the output of uWSGI,

[pid: 20807|app: 0|req: 1/1] 127.0.0.1 () {50 vars in 800 bytes} [Sun Feb 23 13:44:58 2014] POST /TextPickup => generated 6 bytes in 2 msecs (HTTP/1.1 200) 0 headers in 19 bytes (2 switches on core 0)

and even the wireshark: enter image description here It's clear that the backend have send response successfully including the string "test.." I expected,but why the callback function didn't get the response string as its parameter?

UPDATE: in firebug, enter image description here when I right-clicked the http://127.0.0.1/TextPickup and selected "Open in New Tab", a page including the expected string "test.." appeared.however, it is supposed to be the parameter of the callback function.

¿Fue útil?

Solución

I have found why it didn't work -- the web.py is not used correctly,and the browse to the web page is also wrong.

first, I arranged the file like below:

folder1
--GetText.html
--cgi-bin
    --TextAnalysis.py

and accessed the web page by just clicking the file GetText.html. the error happended.

I reviewed some pages about web.py and jQuery,such as random Bytes, even a forum's source code used web.py and jQuery on github, realized the /template folder is not unnecessary.

Now I rearrange the files like this

folder1
--TextAnalysis.py
--templates
    --GetText.html

thus jquery.post() in GetText.html is changed to

jQuery.ajax({
    type: 'POST',
    url: "/TextPickup",
    data: postdata,
    success: function(data){alert(data);},
    dataType:'text'
});

in TextAnalysis.py:

class Index:
    def GET(self):
    return render.GetText()

...

render = web.template.render('templates/') #add up

now I visist the web page by typing url http://127.0.0.1/ in firefox, and all works fine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top