开发者

Twisted.Web and AJAX

开发者 https://www.devze.com 2023-01-10 08:23 出处:网络
I\'ve implemented a toy web service in Twisted.Web: from twisted.web import server, resource, http class RootResource(resource.Resource):

I've implemented a toy web service in Twisted.Web:

from twisted.web import server, resource, http

class RootResource(resource.Resource):
    def __init__(self):
        resource.Resource.__init__(self)
        self.putChild('test', TestHandler())

class TestHandler(resource.Resource):
    isLeaf = True

    def __init__(self):
        resource.Resource.__init__(self)
    def render_GET(self, request):
        return self.render_POST(request)
    def render_POST(self, request):
        return "hello world!"

if __name__ == "__main__":
    import sys
    from twisted.internet import reactor
    reactor.listenTCP(8082, server.Site(RootResource()))
    reactor.run()

According to curl it works fine:

$ curl --url http://localhost:8082/test -v
[..]
< HTTP/1.1 200 OK
< Date: Mon, 02 Aug 2010 11:54:35 GMT
< Content-Length: 13
< Content-Type: text/html
< Server: TwistedWeb/8.2.0
< 
hello world!

Now, I'd like to call the service using the AJAX methods provided by JQuery. Here is the co开发者_开发百科rresponding Java Script code:

[..]
// Submit button
$("#submit").click(function(e){
    $.ajax({type: "POST", 
            url: "http://localhost:8082/test",
            data: {},
            success: function(data) {
              alert("Success:" + data);                  
            }
    });
});
[..]

Although the success callback gets called, data equals null. Does anybody have a clue why?

thanks, Peter


I can't reproduce the issue. I have used your server and your exact ajax call with JQuery and it loads fine. The alert box shows "Success: hello world!" as expected. You must have something else wrong.


dataType is important,if you use dataType: 'jsonp' you should send a callback from server side. i think because of this it works in curl and not work in your browser

0

精彩评论

暂无评论...
验证码 换一张
取 消