我能够通过Ajax调用成功传回JSON并将内容放入一个,但是同样的“echo”语句返回JSON也将“回声”JSON直接“回显”到我的页面,我不想要。我该如何防止这种情况发生?这是我的代码:
我的表格:
<script type="text/javascript" src="includes_js/registration3.js"></script>
的Ajax:
$.ajax({
type: "POST",
url: "includes_php/registration3.php",
data: datastring,
dataType: "json",
success: function(data) {
$('.message').text(data);
}
})
网址中的PHP:
$msgarr[] = "Please enter all Fields";
$json_msg=json_encode($msgarr);
echo $json_msg; //also sends directly to my page
答案 0 :(得分:1)
success: function(data) {
$('.message').text(data);//here is where the magic happens,
//change that line for what you want to do
}
你必须解析json:link
obj=$.parseJSON(data)
您可以通过他们的密钥访问
alert(obj.key1);
在php中:
$msgarr = array("key1"=>"Please enter all Fields");
$json_msg=json_encode(utf8_encode($msgarr));
//utf8 enconde is to avoid invalid format json for characters strangers
echo $json_msg;