jquery POST 方法不起作用?
这是 html 和 jQuery 部分:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
</head>
<body>
<script type="text/javascript">
$('document').ready(function(){
$('#submit').click(function(){
var username=$('#user').val();
$.post('http://localhost:8080/verify/comment.php',
{
user:username
},
function(return_data)
{
alert(return_data);
}
);
});
});
</script>
Username:<input type="text" id="user"/>
<input type="button" id="submit" value="submit"/>
</body>
</html>
comment.php
<?php
echo 'welcome';
?>
它显示一条空警报消息..我无法在警报消息中获取值“欢迎”........
有什么建议吗......?
Here is the html and jQuery part:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
</head>
<body>
<script type="text/javascript">
$('document').ready(function(){
$('#submit').click(function(){
var username=$('#user').val();
$.post('http://localhost:8080/verify/comment.php',
{
user:username
},
function(return_data)
{
alert(return_data);
}
);
});
});
</script>
Username:<input type="text" id="user"/>
<input type="button" id="submit" value="submit"/>
</body>
</html>
comment.php
<?php
echo 'welcome';
?>
It displays an empty alert message.. I can't get the value "welcome" in alert message........
Any suggestion.......?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哦……看着这段代码,我的眼睛简直要疯了。你做错了很多事情......从哪里开始?
submit
按钮上的click
事件,而是侦听表单上的submit
事件。这应该对你有用:
这是你的 PHP,它回显一个
json_encode()
d 字符串,作为上下文(注意我们如何在上面的代码中访问returned_data.message
:Ohhh... my eyes just went crazy rolling by looking at this code. So many things you are doing wrong... Where to start?
click
event on thesubmit
button, but for asubmit
event on the form.This should be working for you:
Here is your PHP, which echos a
json_encode()
d string, for context (notice how we accessedreturned_data.message
on the above code:也许因为 SOP(http://en.wikipedia.org/wiki/Same_origin_policy)
你可以无法通过ajax从不同端口的同一url获取数据。
maybe because of SOP(http://en.wikipedia.org/wiki/Same_origin_policy)
you can not get data from same url with different port through ajax.
清理了一下代码,您可能想查看这篇文章,了解有关在 jquery 中使用 post 函数的一些提示:
http://web.archive.org/web/20160410111221/http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with -php/
此外,其他人也提出了很好的建议,如果可以的话,请使用更新版本的 jquery,并确保代码干净且正确。
Cleaned the code up a bit and you might want to look at this post for some tips on using the post function in jquery:
http://web.archive.org/web/20160410111221/http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/
Also, the other guys had good advice as far as using a newer version of jquery if you can and making sure the code is clean and correct.