php - 即使 PHP 文件设置正确,也没有 Ajax 响应

标签 php jquery html ajax xmlhttprequest

我有一个简单的注册邮件列表表单。它将用户的电子邮件地址发送到 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 :

enter image description here

enter image description here

enter image description here

这是我的代码片段:

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 脚本使用的是 GET 方法,但您的 jQuery 代码使用的是 POST 方法,对吗?

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

祝你好运!

关于php - 即使 PHP 文件设置正确,也没有 Ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9084284/

相关文章:

php - 上传非常大的文件(> 5GB)

javascript - 可以在没有持续回流的情况下动态调整高度的文本区域吗?

javascript - Jquery 附加/前置 svg 并查找子元素

javascript - 按下按钮即可在网站其余部分的顶部播放视频

javascript - 基于动态可见元素数据属性插入 HTML 表格行 "Heading"

jquery - 带有向上/向下箭头的垂直文本框轮播

html - 列表在 firefox 和 internet explorer 上显示错误,但在 chrome 上不显示

javascript .replace 和 .trim 在 vuejs 中不起作用

javascript - 谁能用简单易懂的语言解释 ../和 ./在包含文件时有什么区别?

Javascript true 条件未执行