gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:40:17 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com