JSON编码警报消息 [英] JSON encode alert message
本文介绍了JSON编码警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何alert
json_encode()
消息并重新加载页面?下面的功能仅显示未定义的警报消息
How do I alert
a json_encode()
message and reload the page? The function below only displays an undefined alert message
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();
}
});
});
推荐答案
您正在尝试获取未定义的索引response
You're trying to get a undefined index response
提供您的PHP脚本返回:
Provided that your PHP script returns:
{
"msg": "<your-message-here>"
}
在您的JavaScript中,您可以执行以下操作:
In your javascript you can do it:
$.ajax({
type : "POST",
url: "transferProduct.php",
dataType: 'json',
success : function(response) {
alert(response.msg);
location.reload();
}
});
这篇关于JSON编码警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文