无法让 sweetalert2 ajax 工作
Can't get sweetalert2 ajax to work
我对 ajax 有意见,我正在努力学习,但他不想要我:)
所以这是我的代码:
<script>
$('._givePP').on('click', () => {
swal.mixin({
input: 'text',
confirmButtonText: 'Next →',
showCancelButton: true,
progressSteps: ['1', '2']
}).queue([
{
title: 'Question 1',
text: 'Chaining swal2 modals is easy'
},
{
title: 'Question 2',
text: 'bla bla 2'
}
]).then((result) => {
if (result.value) {
$.ajax({
_dapepe: true,
url: '<?php echo Config::$data->url;?>action/profile',
type: 'POST',
success: (e) => {
e = JSON.parse(e);
swal({
title: e.title,
text: e.message,
type: e.type
});
}
});
}
})
});
</script>
这是 php 代码:
<?php
if(isset($_POST['_dapepe'])) {
return print_r(json_encode(array('title' => 'Success','text' => 'gg','type' =>'success')));
}
?>
我想要得到的只是一个简单的 gg 文本,所以我知道它正在读取 php 文件,但我没有收到 gg 消息。有谁知道出了什么问题或为什么没有'没读正文?
顺便说一句,我验证了 url 它正在工作。
您的 ajax 调用不会向服务器发送任何数据,因此行 if(isset($_POST['_dapepe']))
永远不会是 true
。这反过来意味着您的 PHP 代码永远不会 returns 任何数据,因此您的 JS "success" 代码将崩溃,因为它试图访问不存在的数据。您的浏览器控制台中可能有与此相关的错误或警告,尽管您没有提及它们。
在$.ajax选项中,而不是
_dapepe: true,
你需要写
data: { _dapepe: true },
按照你写的方式,jQuery 不知道你打算 _dapepe
成为一个数据字段,它只是将其视为一个它无法识别的选项,并且因此忽略它。您需要将它放在 data
选项中,这样 jQuery 知道如何处理它。
http://api.jquery.com/jquery.ajax/ 记录了您可以提供给 $.ajax() 方法的所有有效选项。
我也在你的PHP推荐这些东西:
1) return print_r
是一个错误,不会起作用。 print_r 将其数据直接转储到输出,除非您将单独的标志设置为它的第二个参数(参见 http://php.net/manual/en/function.print-r.php)
2) 使用 echo
而不是 print_r
- print_r 是一个调试工具。根据您返回的数据类型,它有时可能会打印实际上不属于您的可变数据的字符(例如 https://eval.in/1040578)。在 json_encode() 的情况下它可能是安全的,但不要养成使用它的习惯。
2) 如果您的代码总是返回 something 会更好,即使它是一个空对象。如果根本没有响应数据,JavaScript 有时会崩溃。
考虑到这一点,我建议将 PHP 更改为:
<?php
$array = array();
if(isset($_POST['_dapepe'])) {
$array = array('title' => 'Success','text' => 'gg','type' =>'success');
}
echo json_encode($array, JSON_FORCE_OBJECT);
?>
我对 ajax 有意见,我正在努力学习,但他不想要我:)
所以这是我的代码:
<script>
$('._givePP').on('click', () => {
swal.mixin({
input: 'text',
confirmButtonText: 'Next →',
showCancelButton: true,
progressSteps: ['1', '2']
}).queue([
{
title: 'Question 1',
text: 'Chaining swal2 modals is easy'
},
{
title: 'Question 2',
text: 'bla bla 2'
}
]).then((result) => {
if (result.value) {
$.ajax({
_dapepe: true,
url: '<?php echo Config::$data->url;?>action/profile',
type: 'POST',
success: (e) => {
e = JSON.parse(e);
swal({
title: e.title,
text: e.message,
type: e.type
});
}
});
}
})
});
</script>
这是 php 代码:
<?php
if(isset($_POST['_dapepe'])) {
return print_r(json_encode(array('title' => 'Success','text' => 'gg','type' =>'success')));
}
?>
我想要得到的只是一个简单的 gg 文本,所以我知道它正在读取 php 文件,但我没有收到 gg 消息。有谁知道出了什么问题或为什么没有'没读正文?
顺便说一句,我验证了 url 它正在工作。
您的 ajax 调用不会向服务器发送任何数据,因此行 if(isset($_POST['_dapepe']))
永远不会是 true
。这反过来意味着您的 PHP 代码永远不会 returns 任何数据,因此您的 JS "success" 代码将崩溃,因为它试图访问不存在的数据。您的浏览器控制台中可能有与此相关的错误或警告,尽管您没有提及它们。
在$.ajax选项中,而不是
_dapepe: true,
你需要写
data: { _dapepe: true },
按照你写的方式,jQuery 不知道你打算 _dapepe
成为一个数据字段,它只是将其视为一个它无法识别的选项,并且因此忽略它。您需要将它放在 data
选项中,这样 jQuery 知道如何处理它。
http://api.jquery.com/jquery.ajax/ 记录了您可以提供给 $.ajax() 方法的所有有效选项。
我也在你的PHP推荐这些东西:
1) return print_r
是一个错误,不会起作用。 print_r 将其数据直接转储到输出,除非您将单独的标志设置为它的第二个参数(参见 http://php.net/manual/en/function.print-r.php)
2) 使用 echo
而不是 print_r
- print_r 是一个调试工具。根据您返回的数据类型,它有时可能会打印实际上不属于您的可变数据的字符(例如 https://eval.in/1040578)。在 json_encode() 的情况下它可能是安全的,但不要养成使用它的习惯。
2) 如果您的代码总是返回 something 会更好,即使它是一个空对象。如果根本没有响应数据,JavaScript 有时会崩溃。
考虑到这一点,我建议将 PHP 更改为:
<?php
$array = array();
if(isset($_POST['_dapepe'])) {
$array = array('title' => 'Success','text' => 'gg','type' =>'success');
}
echo json_encode($array, JSON_FORCE_OBJECT);
?>