我创建了一个 web.py URL,它只提供了 2 个文本框(没有提交按钮)。当我在第一个文本字段中输入数字时,它应该将该值发布到另一个 URL 中。通过使用 AJAX post()
我想将数据发布到第二个 URL。第二个 URL 应该返回我发布的数据,并且它应该显示在第二个文本框中。如何使用 key up 函数和 AJAX post()
方法解决这个问题?我尝试了很多 post()
方法,但找不到合适的解决方案。
最佳答案
我已经找到了解决方案,感谢您的所有建议,我已在此处附加了完整的代码以及建议的更改。
import web
urls = (
'/', 'first','/home','second')
app = web.application(urls, globals(), True)
class first:
def GET(self):
return'''
<html>
<head>
<script type="text/javascript"src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#text1").keyup(function(){
var txt = $("#text1").val();
$.post(
"http://127.0.0.1:8080/home",
{text1 : txt},
function(result){
$("#text2").val(result);
});
});
});
</script>
</head>
<body>
Enter your text here : <input type="text" id="text1" name="text1"/>
You entered : <input type="text" id="text2"/>
</body>
</html>'''
class second:
def GET(self):
return "Entered value goes here"
def POST(self):
i = web.input()
n = i.text1
return n
if __name__ == "__main__":
app.run()
关于jquery - 在 AJAX 中使用 .post() 方法将数据发布到另一个 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913470/