如何提醒
json_encode()
消息并重新加载页面?下面的函数仅显示未定义的警报消息
if($result1 == false)
$response['msg'] = "Transfer failed due to a technical problem. Sorry.";
else
$response['msg'] = "Successfully transferred";
echo json_encode($response);
$("#transfer").click(function() {
$.ajax({
type : "POST",
url : "transferProduct.php",
data : {},
success : function(data) {
data = $.parseJSON(data);
alert(data.response);
location.reload();
}
});
});
最佳答案
您正在尝试获取 undefined index 响应
假设您的 PHP 脚本返回:
{
"msg": "<your-message-here>"
}
在你的 JavaScript 中你可以做到这一点:
$.ajax({
type : "POST",
url: "transferProduct.php",
dataType: 'json',
success : function(response) {
alert(response.msg);
location.reload();
}
});
关于javascript - JSON 编码警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40999171/