如何alert
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();
}
});
});
答案 0 :(得分:1)
您正在尝试获取未定义的索引response
前提是您的PHP脚本返回:
{
"msg": "<your-message-here>"
}
在您的javascript中,您可以这样做:
$.ajax({
type : "POST",
url: "transferProduct.php",
dataType: 'json',
success : function(response) {
alert(response.msg);
location.reload();
}
});
答案 1 :(得分:1)
以这种方式使用代码
transferProduct.php
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) {
datas = $.parseJSON(data);
alert(datas.msg);
location.reload();
}
});
});
或者您可以使用$ .getJSON代替$ .ajax
$("#transfer").click(function() {
$.getJSON("transferProduct.php",function (data){
alert(data.msg);
location.reload();
});
});
答案 2 :(得分:-1)
我认为您应该将返回格式设置为在后台正确识别:
{ “成功”:真, “消息”: “---------”}
然后在JavaScript中:data.message