jquery POST方法不起作用?

时间:2011-06-23 12:59:41

标签: jquery

这是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';
 ?>

它显示一条空的警告信息..我无法在警告信息中获得值“welcome”........
有什么建议.......?

3 个答案:

答案 0 :(得分:2)

可能是因为SOP(http://en.wikipedia.org/wiki/Same_origin_policy)

您无法通过ajax从不同端口获取相同网址的数据。

答案 1 :(得分:2)

噢......看着这段代码,我的眼睛疯了。你做错了很多事......从哪里开始?

  1. 当前的jQuery版本是1.6,而不是1.3.2。
  2. 您的HTML无效。你没有表单元素。
  3. 您不会在click按钮上收听submit事件,而是在表单上收听submit事件。
  4. 这应该适合你:

    <html>
        <head>
        </head>
        <body>
            <script type="text/javascript" src="jquery-1.3.2.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    $('#login').submit(function(e){
                        // prevent form submit, so we can do a manual one
                        e.preventDefault();
    
                        var username = $('#user').val();
                        $.post('http://localhost:8080/verify/comment.php', {user:username}, function(return_data){
                            alert(return_data.message);
                        }, 'json');
                    });
                });
            </script>
            <form id="login" action="" method="post">
                <label for="user">Username:</label>
                <input type="text" id="user"/>
                <input type="button" id="submit" value="submit"/>
            </form>
        </body>
    </html>
    

    以下是您的PHP,其中包含一个json_encode() d字符串,用于上下文(请注意我们如何访问上述代码returned_data.message

    <?php
    $return_data = array(
        'message' => 'Welcome'
    );
    echo json_encode($return_data);
    ?>
    

答案 2 :(得分:0)

稍微清理一下代码,你可能想看看这篇文章,了解在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提出了很好的建议,并确保代码干净正确。

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">

$(function(){

$('#submit').submit(function(e){
    e.preventDefault();

    var username=$('#user').val();

    $.post('/verify/comment.php',{user:username},function(return_data){
    alert(return_data);
    });
});

});
</script>
</head>
<body>
<form>

Username:<input type="text" id="user"/>

<input type="button" id="submit" value="submit"/>
</form>
</body>
</html>
相关问题