Javascript Twisted:我无法获得Ajax;“数据”;参数
使用以下参数触发ajax“POST”:Javascript Twisted:我无法获得Ajax;“数据”;参数,javascript,ajax,twisted,Javascript,Ajax,Twisted,使用以下参数触发ajax“POST”: function test22(portnb){ console.log(portnb) $.ajax({url: "action", dataType : 'html', type: "POST", data: portnb, success: function( strData2 ){; console.log(str
function test22(portnb){
console.log(portnb)
$.ajax({url: "action",
dataType : 'html',
type: "POST",
data: portnb,
success: function( strData2 ){;
console.log(strData2);
$("#content3").html(strData2);
}
});
};
它由TwistedPython脚本处理:(参见下面的interresting部分)
class Test3Handler(resource.resource):
isLeaf=True
定义初始化(自):
resource.resource.\uuuuu init\uuuuu(self)
def render_POST(自我,请求):
argo=request.content.getvalue()
打印(argo)
retp=“- ”
retp+=argo
retp+=“
”
打印(retp)
返回retp
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
从twisted.internet导入
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(根))
反应堆运行()
问题是我无法获取Ajax发送的“data”参数(data:portnb)。变量“argo”为空 我是Python/Ajax新手。 你能帮我解决这个问题吗? 以后开发更复杂的东西会很有帮助。 谢谢
Gilles您应该将数据类型设置为
json
,并在数据字段中发送一个对象,如{params:portnb}
portnb=23。当Ajax显示“data:{portnb}”时,python脚本将收到“portnb=23”。我只需要得到23,而不是portnb=23。有没有一个简单的方法可以做到这一点?谢谢,Gillesportnb的值是多少?data:{portnb:portnb}
尝试将其作为数据发送,并在另一端以post的形式获取值“您应该将数据类型设置为json”-绝对不是。服务器端代码显然返回的是HTML而不是JSON。“发送类似{params:portnb}的对象”-为什么?服务器端代码正在尝试读取原始邮件正文。URL编码数据有什么用?(假设portnb
还不是一个对象)谢谢你的帮助portnb=23-我更喜欢使用“html”作为数据类型,因为最后,我希望在html页面中很好地显示它。我已经按照你的建议做了,而且很有效。这是最后一个ajax代码:dataType:'html',type:'POST',data:{param:portnb},我在mre上工作了一个多星期,但没有成功,现在它可以工作了。各位,你们和这个论坛是最好的。非常感谢你,Gilles还有一件事,还有一件事。数据Ajax参数是DATA:{portnb},portnb=23。“success:function(strData2){;”返回:portnb=23。我只需要portnb(23)。我如何从“portnb=23”中提取23。理想情况下,我希望在扭曲的一侧执行此操作。我检查了Python语句,但我迷路了。我必须使用JSON吗?谢谢。Gilles
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()