【问题标题】:Twisted : I cannot get the Ajax "data" parameterTwisted:我无法获取 Ajax“数据”参数
【发布时间】:2016-08-28 08:59:55
【问题描述】:

使用这些参数触发 ajax“POST”:

function test22(portnb){
    console.log(portnb)
    $.ajax({url: "action",
            dataType : 'html',
            type: "POST",
            data: portnb,
            success: function( strData2 ){;
            console.log(strData2);
            $("#content3").html(strData2);
            }
    });
};

它是由一个扭曲的 python 脚本处理的:(见下面有趣的部分)

class Test3Handler(resource.Resource):
    isLeaf = True

    def __init__(self):
        resource.Resource.__init__(self)
    def render_POST(self, request):
       argo = request.content.getvalue()
       print( argo )
       retp = "<ul><li>"
       retp += argo
       retp += "</ul>"
       print (retp)
       return retp
if __name__ == "__main__":
    import sys
    from twisted.internet import reactor
    testHandler = TestHandler()
    test2Handler = Test2Handler()
    test3Handler = Test3Handler()
    root = static.File('/home/pi/web4')
    root.putChild('test', testHandler)
    root.putChild('test2', test2Handler)
    root.putChild('action', test3Handler)
    reactor.listenTCP(8082, server.Site(root))
    reactor.run()

问题是我无法获取 Ajax 发送的“数据”参数(数据:portnb)。变量“argo”为空。

我是 Python/Ajax 的新手。 你能帮我解决这个问题吗? 以后开发更复杂的东西会很有帮助。 谢谢 吉尔斯

【问题讨论】:

  • portnb的值是多少?
  • 尝试将其作为data: {portnb:portnb} 发送并在另一端获取值作为帖子

标签: javascript ajax twisted


【解决方案1】:

portnb = 23。当 Ajax “data : {portnb}”时,python 脚本接收“portnb = 23”。我只需要得到 23,而不是 portnb = 23。有没有简单的方法可以做到这一点?谢谢,吉尔斯

【讨论】:

    【解决方案2】:

    您应该将 dataType 设置为 json 并在数据字段中发送一个类似 { params: portnb } 的对象

    【讨论】:

    • "您应该将 dataType 设置为 json" — 绝对不是。服务器端代码显然返回的是 HTML 而不是 JSON。
    • “发送一个像 { params: portnb } 这样的对象”——为什么?服务器端代码正在尝试读取原始 POST 正文。 URL 编码数据有什么用? (这是假设 portnb 还不是一个对象)
    • 感谢您的帮助。 - portnb = 23 - 我更喜欢使用“html”作为数据类型,因为最后我想在 html 页面中很好地显示它。我已经按照你的建议做了,它有效。这是最终的 ajax 代码:dataType : 'html', type: "POST", data: {param : portnb},我在它上面工作了超过 1 周没有成功,现在它可以工作了。伙计们,你和这个论坛是最好的。非常感谢 问候,吉尔斯
    • 还有一件事。
    • 还有一件事。 DATA Ajax 参数是 data: {portnb}, Portnb = 23。 "success: function( strData2 ){;"返回:portnb=23。我只需要portnb(23)。如何从“portnb = 23”中提取23。理想情况下,我想在扭曲的一面做。我检查了 Python 语句,但我迷路了。我必须使用 JSON 吗?谢谢。吉尔斯
    猜你喜欢
    • 2013-07-27
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多