Twisted.Web和AJAXTwisted、Web、AJAX

2023-09-10 14:28:55 作者:世界小到让我们相遇

我在Twisted.Web实施的玩具Web服务:

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!

现在,我想打电话给使用的jQuery提供的AJAX方法的服务。 下面是相应的Java脚本code:

Now, I'd like to call the service using the AJAX methods provided by JQuery. Here is the corresponding 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?

谢谢,  彼得·

推荐答案

我无法重现该问题。我已经使用你的服务器,并使用jQuery您的具体Ajax调用和它加载的罚款。该警告框显示成功:世界,你好!如预期。你必须有别的东西是错误的。

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.