【问题标题】:No Ajax response even though PHP file set up correctly即使 PHP 文件设置正确,也没有 Ajax 响应
【发布时间】:2012-02-23 10:50:56
【问题描述】:

我有一个简单的注册邮件列表表格。它将用户的电子邮件地址发送到 store-address.php 文件。我使用 jQuery 的 ajax 对象向 php 文件发送请求,然后接收响应。

问题是我没有从 php 文件中得到响应。我尝试在请求中将缓存设置为 false。我也尝试通过这样的 URL 发送信息:

http://www.fifthtribe.com/inc/store-address.php?ajax=true&cache=false&email=test4%40gmail.com

当我这样做时,它会起作用并给我一个响应。但是,当我通过 ajax 执行此操作时,它并没有给我回应。这是来自 Firebug:

这是我的代码中的 sn-ps:

HTML:

<div id="mlist">
<form id="mlist_form" method="POST" action="">
    <input type="text" id="email" name="email" placeholder="Email" />
    <input type="submit" id="submit_btn" value="Join" />
</form>
<div id="response"></div>
</div>

JQuery:

/* Add to mailing list */           
$("#mlist_form").submit( function(e){
    //$('#response').append('<div id="thanks-mce"><div id="mce-arrow"></div>Thanks for signing up!</div>');
    var email = escape( $('#email').val() );
    e.preventDefault();

    data = {
        "ajax" : "true",
        "email" : email,
        "cache" : "false"
}

    $.ajax({ 
            type: "POST",           
            url: 'inc/store-address.php',
            data: data,
            success: function( msg ){
                // successfully signed up
                $('#response').html( msg );
                $('#email').val('');
            },
            error: function( err ){
                // error while signing up
                $('#response').html('Error: Is your email correct?');
            }
    });

    return false;

});

PHP: 函数 storeAddress(){

    // Validation
    if(!$_GET['email']){ return "No email address provided"; } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }

    require_once('MCAPI.class.php');
    // grab an API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us4');

    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    $list_id = "xxxxxxxx";

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
            // An error ocurred, return error message   
            return 'Error: ' . $api->errorMessage;
    }

}

    // If being called via ajax, autorun the function
    if($_GET['ajax']){ echo storeAddress(); }
?>

【问题讨论】:

  • 在 php 代码中尝试 print_r($_REQUEST); 并确保您的数据在那里

标签: php jquery html ajax xmlhttprequest


【解决方案1】:

看起来您使用的是 $_GET 而不是 $_POST。尝试回显 $_REQUEST 的内容以查看其中包含的内容。

【讨论】:

    【解决方案2】:

    您意识到您的 PHP 脚本正在使用 GET 方法,但您的 jQuery 代码正在使用 POST 方法,对吗?

    如果信息被发布到 PHP,PHP 将需要使用$_POST 来检索它。这就解释了为什么使用 $_GET 的 URL 方法有效,而 jQuery POST 无效。

    祝你好运!

    【讨论】:

    • 谢谢。我知道这一定是我忽略的东西。获得第二双眼睛总是有帮助的。
    【解决方案3】:

    调试你的脚本!

    在脚本的成功和错误部分放置警报,然后您就会知道 AJAX 是否正常工作。

    如果没有,您可以在文档中查找问题所在。

    另外,这里的错误也很简单。您在 PHP 中使用 $_GET 并且使用 AJAX 发布数据,这不会显示错误。虽然 PHP 文档不会处理您的请求,因为它没有被提供任何参数。

    【讨论】:

      猜你喜欢
      • 2013-06-29
      • 2016-10-13
      • 2021-08-23
      • 2017-10-27
      • 2011-01-21
      • 2021-08-04
      • 1970-01-01
      • 2020-03-09
      • 2015-09-02
      相关资源
      最近更新 更多