Ajax Success 函数被忽略 - 代码仍然运行
Ajax Success Function Being Ignored - Code Still Runs
我正在尝试 post 通过 AJAX 向黑猩猩帐户发送邮件。如果调用成功,我想重定向到一个URL。经过多次测试,调用每次都成功,但我无法获得重定向...就好像代码运行然后跳过 if "result = success",直接转到我的 else 语句,这只是警报。当我查看返回的数据时,我得到 "nullsuccess"。我什至尝试过 if result = "nullsuccess" 并且它仍然适用于 else 语句。
$("#mc-embedded-subscribe-form").submit(function(e) {
var url = $(this).prop('action'); // the script where you handle the form input.
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(), // serializes the form's elements.
dataType: "text",
success: function(data){
if(data.result == 'success'){
window.location.href = 'https://google.com';
}else{
alert("ERROR-ONLY IT STILL SUBMITS DATA!");
}
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
以上数据类型 - 我使用了 "text",因为如果我使用 "json" 或 "jsonp",它会完全忽略成功函数,包括 if 和 else 语句。有趣的是所有三种数据类型仍然会post数据成功。
这里是 PHP(update-member.php)
$apikey = 'myAPIKEYHERE';
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $_POST['email_address'],
'status' => 'subscribed',
'merge_fields' => array(
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'MYMAILCHIMPURL');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
echo json_encode($response_array);
$result = curl_exec($ch);
$message = curl_errno($ch) === CURLE_OK ? 'success' : 'failure';
echo $message; //echos nullsuccess
我觉得我没有从 update-member.php 返回正确的东西,或者我没有在我的 ajax 成功函数中正确地收集它。
那是因为你可能没有设置result = success。默认情况下它已经在成功功能中。您必须在服务器端进行如下设置。
$data['result'] = 'success';
并且在 ajax 中,您必须将数据类型设置为 'json'
$("#mc-embedded-subscribe-form").submit(function(e) {
var url = $(this).prop('action'); // the script where you handle the form input.
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(), // serializes the form's elements.
dataType: "json", //CHANGED
success: function(data){
if(data.result == 'success'){
window.location.href = 'https://google.com';
}else{
alert("ERROR-ONLY IT STILL SUBMITS DATA!");
}
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
正如评论中所讨论的,您的 PHP 反映了两件事:
echo json_encode($response_array);
// ...
echo $message;
所以脚本 return 有 2 个字符串,连接在一起 - 这解释了您看到的 nullsuccess
奇怪的样子。去掉第一个 echo
,这样你就只 return 一个单一的纯文本状态词。
下一个问题在你的Javascript。您已指定您的 AJAX 调用将 return 一些文本:
dataType: "text",
事实上它确实如此 - 纯文本 success
或 failure
状态消息。但是在使用该响应时,您的代码将其视为 JSON:
if(data.result == 'success'){
data
是您的纯文本 response/status 消息,它没有 .result
属性。它只是文本,所以将其更改为:
if(data === 'success'){
更新
这是您代码的工作 简化版本。我在本地设置并验证它有效。我所做的 只有 事情就是我上面描述的 - 确保 dataType
与 PHP return 相匹配,并确保JS在拿到的时候就是这么对待的。在这种情况下,它一直是 text
。
复制此代码并尝试。在您的电子邮件字段中输入 "a@a.com" 以测试成功响应,以及其他任何内容以测试失败。
Javascript
$("#mc-embedded-subscribe-form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(),
dataType: "text", // <-- WE EXPECT TO GET TEXT BACK
success: function(data) {
// Check what your PHP returns, make sure no newlines
console.log(data);
if (data === 'success') { // <-- TREAT WHAT WE GOT AS TEXT
window.location.href = 'https://google.com';
} else {
alert("Error");
}
}
});
});
PHP
<?php
// The problems you are having are unrelated to the Mailchimp processing,
// so let's simplify it to get the JS side working. Make sure you have
// no blank lines before or after your opening/closing PHP tags, as
// they will show up in the output, and your JS would then see something
// like "\nsuccess" for example. Also make sure nothing else in your PHP
// generates any output.
// WE RETURN TEXT
echo ($_POST['email_address'] === 'a@a.com') ? 'success' : 'failure';
?>
如果您尝试此操作但不起作用,则说明您在某个地方遇到了另一个问题。打开浏览器的开发工具,然后检查控制台。查看网络请求并查看POST。
解决您的一些评论:
通过调整问题的随机无关部分,你就是 over-complicating 东西。在这个特定问题中,您只需要对齐几个剩余部分(其他部分已经工作):
1) 如果你告诉你的 JS 你的 PHP 将要 return 文本,它必须 return 文本。
2) 如果您告诉您的 JS 您的 PHP 将转为 return 文本,那么您的 JS 必须将您的 PHP return 视为文本。
就是这样。真的没什么了。
GET vs POST:convention and standards是使用GET获取和查看数据,POST是更改 数据。您正在订阅一个电子邮件地址 - 这是一个 更改 ,您应该使用 POST.
GET 或 POST 对您的 PHP return 没有任何影响,none,无论它运行正常,还是 dataType
] 你应该使用。它们是不相关的拼图(尽管这并不意味着您使用什么无关紧要)。
我正在尝试 post 通过 AJAX 向黑猩猩帐户发送邮件。如果调用成功,我想重定向到一个URL。经过多次测试,调用每次都成功,但我无法获得重定向...就好像代码运行然后跳过 if "result = success",直接转到我的 else 语句,这只是警报。当我查看返回的数据时,我得到 "nullsuccess"。我什至尝试过 if result = "nullsuccess" 并且它仍然适用于 else 语句。
$("#mc-embedded-subscribe-form").submit(function(e) {
var url = $(this).prop('action'); // the script where you handle the form input.
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(), // serializes the form's elements.
dataType: "text",
success: function(data){
if(data.result == 'success'){
window.location.href = 'https://google.com';
}else{
alert("ERROR-ONLY IT STILL SUBMITS DATA!");
}
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
以上数据类型 - 我使用了 "text",因为如果我使用 "json" 或 "jsonp",它会完全忽略成功函数,包括 if 和 else 语句。有趣的是所有三种数据类型仍然会post数据成功。
这里是 PHP(update-member.php)
$apikey = 'myAPIKEYHERE';
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $_POST['email_address'],
'status' => 'subscribed',
'merge_fields' => array(
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'MYMAILCHIMPURL');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
echo json_encode($response_array);
$result = curl_exec($ch);
$message = curl_errno($ch) === CURLE_OK ? 'success' : 'failure';
echo $message; //echos nullsuccess
我觉得我没有从 update-member.php 返回正确的东西,或者我没有在我的 ajax 成功函数中正确地收集它。
那是因为你可能没有设置result = success。默认情况下它已经在成功功能中。您必须在服务器端进行如下设置。
$data['result'] = 'success';
并且在 ajax 中,您必须将数据类型设置为 'json'
$("#mc-embedded-subscribe-form").submit(function(e) {
var url = $(this).prop('action'); // the script where you handle the form input.
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(), // serializes the form's elements.
dataType: "json", //CHANGED
success: function(data){
if(data.result == 'success'){
window.location.href = 'https://google.com';
}else{
alert("ERROR-ONLY IT STILL SUBMITS DATA!");
}
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
正如评论中所讨论的,您的 PHP 反映了两件事:
echo json_encode($response_array);
// ...
echo $message;
所以脚本 return 有 2 个字符串,连接在一起 - 这解释了您看到的 nullsuccess
奇怪的样子。去掉第一个 echo
,这样你就只 return 一个单一的纯文本状态词。
下一个问题在你的Javascript。您已指定您的 AJAX 调用将 return 一些文本:
dataType: "text",
事实上它确实如此 - 纯文本 success
或 failure
状态消息。但是在使用该响应时,您的代码将其视为 JSON:
if(data.result == 'success'){
data
是您的纯文本 response/status 消息,它没有 .result
属性。它只是文本,所以将其更改为:
if(data === 'success'){
更新
这是您代码的工作 简化版本。我在本地设置并验证它有效。我所做的 只有 事情就是我上面描述的 - 确保 dataType
与 PHP return 相匹配,并确保JS在拿到的时候就是这么对待的。在这种情况下,它一直是 text
。
复制此代码并尝试。在您的电子邮件字段中输入 "a@a.com" 以测试成功响应,以及其他任何内容以测试失败。
Javascript
$("#mc-embedded-subscribe-form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(),
dataType: "text", // <-- WE EXPECT TO GET TEXT BACK
success: function(data) {
// Check what your PHP returns, make sure no newlines
console.log(data);
if (data === 'success') { // <-- TREAT WHAT WE GOT AS TEXT
window.location.href = 'https://google.com';
} else {
alert("Error");
}
}
});
});
PHP
<?php
// The problems you are having are unrelated to the Mailchimp processing,
// so let's simplify it to get the JS side working. Make sure you have
// no blank lines before or after your opening/closing PHP tags, as
// they will show up in the output, and your JS would then see something
// like "\nsuccess" for example. Also make sure nothing else in your PHP
// generates any output.
// WE RETURN TEXT
echo ($_POST['email_address'] === 'a@a.com') ? 'success' : 'failure';
?>
如果您尝试此操作但不起作用,则说明您在某个地方遇到了另一个问题。打开浏览器的开发工具,然后检查控制台。查看网络请求并查看POST。
解决您的一些评论:
通过调整问题的随机无关部分,你就是 over-complicating 东西。在这个特定问题中,您只需要对齐几个剩余部分(其他部分已经工作):
1) 如果你告诉你的 JS 你的 PHP 将要 return 文本,它必须 return 文本。
2) 如果您告诉您的 JS 您的 PHP 将转为 return 文本,那么您的 JS 必须将您的 PHP return 视为文本。
就是这样。真的没什么了。
GET vs POST:convention and standards是使用GET获取和查看数据,POST是更改 数据。您正在订阅一个电子邮件地址 - 这是一个 更改 ,您应该使用 POST.
GET 或 POST 对您的 PHP return 没有任何影响,none,无论它运行正常,还是
dataType
] 你应该使用。它们是不相关的拼图(尽管这并不意味着您使用什么无关紧要)。