jquery POST 方法不起作用?

发布于 2024-11-17 02:31:35 字数 948 浏览 0 评论 0原文

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-11-24 02:31:35

哦……看着这段代码,我的眼睛简直要疯了。你做错了很多事情......从哪里开始?

  1. 当前 jQuery 版本是 1.6,而不是 1.3.2。
  2. 您的 HTML 无效。您没有表单元素。
  3. 您不会侦听 submit 按钮上的 click 事件,而是侦听表单上的 submit 事件。

这应该对你有用:

<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);
?>

Ohhh... my eyes just went crazy rolling by looking at this code. So many things you are doing wrong... Where to start?

  1. Current jQuery version is 1.6, not 1.3.2.
  2. Your HTML is not valid. You don't have a form element.
  3. You don't listen for a click event on the submit button, but for a submit event on the form.

This should be working for you:

<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>

Here is your PHP, which echos a json_encode()d string, for context (notice how we accessed returned_data.message on the above code:

<?php
$return_data = array(
    'message' => 'Welcome'
);
echo json_encode($return_data);
?>
月牙弯弯 2024-11-24 02:31:35

也许因为 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.

錯遇了你 2024-11-24 02:31:35

清理了一下代码,您可能想查看这篇文章,了解有关在 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>

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.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文