阿贾克斯POST不工作/双绞线 [英] Ajax POST doesnt work / Twisted

查看:146
本文介绍了阿贾克斯POST不工作/双绞线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Twisted.Web和AJAX 类似的线程已经退出。我甚至把code从那里,但我有同样的问题,在扭曲的服务器用得好好的,但无法弄清楚,为什么我不能用Ajax获取它。在类似的主题,他说,警报出来,但没有数据。对我来说,即使警报不弹出,另一个AJAX功能的工作原理,所以一般用Ajax是一切OK,但正好与取不顺心的事。

Twisted.Web and AJAX Similar thread already exits . I even took code from there , yet i have the same problem , the twisted server works like a charm, but can't figure out why i can't fetch it with ajax. In similar thread he says that alert comes out , but without data. For me even alert doesn't pop up , yet another ajax functions works , so in general with ajax is everything ok , but exactly with fetching something goes wrong.

由于类似的线程也说我可以卷曲获取它 - $卷曲--url HTTP: //本地主机:8082 /测试 -v ,它显示的Hello World,所以服务器工作正常,100%

As also said in similar thread i can fetch it with curl - $ curl --url http://localhost:8082/test -v , and it shows hello world , so servers works fine 100 % .

任何想法?

<script type="text/javascript">
// Submit button
$(function(){
  $.ajax({type: "POST", 
     $('a').click(function(){
        url: "http://localhost:8082/test",
        data: {},
        success: function(data) {alert("Success:" + data);}
    });
  });
});
</script>

<html>
[...]
  <a href="#">Load Favorites Movies</a>...
[...]
</html>

server.py

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


非常感谢您为彼得·乐贝克 Darkporter 。 彼得·乐贝克asnwer标记为正确的,Darkporter从我投了=)。


Big thank you to Peter Le Bek and Darkporter. Peter Le Bek asnwer marked as correct , and Darkporter vote up =) from me .

答:彼得的回答开箱,只是弄得我一点点是行了,在这里你必须指定的静态文件夹中的事情。这是很容易...只是sepcify任何文件夹那里,放在那里index.html,然后将根目录下,当你访问它在网络上。

Answer : Peter's answer works out of the box , just the thing that confused me a little bit was the line , where you had to specify the static folder. It is easy ... just sepcify any folder there , put there index.html and it will a root directory , when you access it on the web.

推荐答案

JavaScript是错位的,试试这个:

Your javascript is mangled, try this:

wwwdir / index.html的

<html>
 <head>
  <script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
 </head>
 <body>
  <a href="#">click me</a>
  <script type="text/javascript">
   $('a').click(function(){
    $.ajax({type: "POST",
            url: "http://localhost:8082/test",
            data: {},
            success: function(data) { alert("Success: " + data); }
    });
   });
  </script>
 </body>
</html>

您可能会仍满足跨域的HTTP请求的限制通过darkporter提到,要解决这个服务你的网页使用相同的扭曲服务器

You'll probably still meet the cross-domain HTTP request restriction mentioned by darkporter, to solve this serve your webpage using the same Twisted server:

server.py

from twisted.web import server, resource, http, static

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

    root = static.File('/path/to/wwwdir')
    testHandler = TestHandler()
    root.putChild('test', testHandler)
    reactor.listenTCP(8082, server.Site(root))
    reactor.run()

这篇关于阿贾克斯POST不工作/双绞线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆