【问题标题】:Javascript (jQuery) - Problem with $.postJavascript (jQuery) - $.post 的问题
【发布时间】:2011-05-19 18:22:14
【问题描述】:

所以,出于某种原因,我的脚本拒绝工作,尽管它似乎是正确的。

我尝试改用 $.ajax,但也没有使用它。任何想法出了什么问题?

<script>
$(document).ready(function() {
     $('#saveForm .submit').click(function() {
      var _user = $('#saveForm .user');
       _userId = $('#saveForm .userId');
       _password = $('#saveForm .password');

      $('#saveForm').append('<p>Loading...</p>');

      $.post("ajax/fetchPhotos.php", {user:_user, userId:_userId, password:_password}, function(data) {
       alert(data);
      });

      return false;
     });
});
</script>

在 ajax/fetchPhotos.php 我有这个:

<?php
set_time_limit(0);
session_start();
require_once("includes/functions.php");
require_once("includes/pclzip.lib.php");

/*
Huge block of code here (commented out for the moment)
*/
echo "wut?";

所以,当点击 .submit 时,它应该向 fetchPhotos.php 发送一个带有三个参数的请求,然后警告“wut?”。现在页面挂在 chrome 中.. 没有任何反应。在 Firefox 中,页面只是刷新。我确实在控制台中遇到了一个错误,但当页面直接刷新时,我只看到它一瞬间。

谢谢, 皮特

【问题讨论】:

  • 你的标记是什么样的?
  • 在 Firefox 中,你看过Error Console吗?这是在Tools 菜单中。 Error Console 中的信息不会消失,除非您手动清除它。此外,如果您查看托管 PHP 脚本的 Web 服务器的日志,您是否看到对您的脚本发出的任何请求?此外,Firefox 上的 HttpFox 扩展将帮助您跟踪请求和响应。
  • @Nick,我不明白你为什么需要标记。不过这很简单,我有一个带有三个输入的表单。 @Ayaz,我不知道!谢谢。这是我在错误控制台中看到的:heypasteit.com/clip/Q0Y

标签: php javascript jquery ajax post


【解决方案1】:

您必须使用 .val() 方法来获取输入的值。

试试这个方法:

<script>
$(document).ready(function() {

    $('#saveForm .submit').bind('click', function() {
        var 
            user = $('#saveForm .user').val(),
            id = $('#saveForm .userId').val(),
            password = $('#saveForm .password').val();

        $('#saveForm').append('<p>Loading...</p>');

        $.post("ajax/fetchPhotos.php", {user:user, userId:id, password:password}, function(data) { 
            alert(data);
        });

        return false;
    });
});
</script>

【讨论】:

    【解决方案2】:

    似乎语法相关的问题,也可以尝试使用绝对 url 或者可以这样做

    new Ajax.Request("ajax/fetchPhotos.php", {
                method: 'post',
                parameters: 'id='+id,
                onComplete: function(transport) {
                alert(transport.responseText);
                }
                });
    

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多