【问题标题】:Twisted.Web and AJAXTwisted.Web 和 AJAX
【发布时间】:2010-08-02 12:13:00
【问题描述】:

我在 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()

根据 curl 它可以正常工作:

$ 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 Script 代码:

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

虽然调用了success 回调,但data 等于null。有人知道为什么吗?

谢谢, 彼得

【问题讨论】:

  • 可能是您的浏览器不允许跨域ajax 的东西。你用的是什么浏览器?

标签: python ajax twisted


【解决方案1】:

我无法重现该问题。我已经使用了你的服务器和你的 JQuery 确切的 ajax 调用,它加载得很好。警报框显示“成功:你好世界!”正如预期的那样。你一定有其他问题。

【讨论】:

    【解决方案2】:

    dataType 很重要,如果你使用 dataType: 'jsonp' 你应该从服务器端发送一个回调。我认为正因为如此,它在 curl 中有效,而在您的浏览器中无效

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      相关资源
      最近更新 更多