Home >Backend Development >PHP Tutorial >javascript - Help! ! ! About mui ajax unable to obtain background data
The js code is as follows
<code> mui.init(); mui.ajax('ajax.php', { dataType: 'json', //服务器返回json格式数据 type: 'post', //HTTP请求类型 headers: { 'Content-Type': 'application/json' }, success: function(data) { alert(data)//就像获取后台php的数据啊!!! }, async:true, error: function(xhr, type, errorThrown) { alert(errorThrown);//好像报类型错误?php那边是json格式啊 } }); </script> </code>
php code is as follows
error_reporting(E_ALL ^ E_DEPRECATED);
//header("Content-type: text/html; charset=utf-8");
header('Content-type: text/json ; charset=utf-8');
//header('Content-type: application/json');
//There is a bit of doubt about this header~ I have tried everything but it still doesn’t work
$arr = array(
'name' => '我是姓名',
'age' => '18'
);
$json_string = json_encode($arr);
echo ($json_string);//The output here is the json string~~
?>
On the mobile phone, an error is reported~But the browser can obtain the data normallyBut it is not working properlyPlease solve it~I just want to get the json data in php~~
Reply content:
The js code is as follows
<code> mui.init();
mui.ajax('ajax.php', {
dataType: 'json', //服务器返回json格式数据
type: 'post', //HTTP请求类型
headers: {
'Content-Type': 'application/json'
},
success: function(data) {
alert(data)//就像获取后台php的数据啊!!!
},
async:true,
error: function(xhr, type, errorThrown) {
alert(errorThrown);//好像报类型错误?php那边是json格式啊
}
});
</script>
</code>
php code is as follows
error_reporting(E_ALL ^ E_DEPRECATED);
//header("Content-type: text/html; charset=utf-8");
header('Content-type: text/json ; charset=utf-8');
//header('Content-type: application/json');
//There is a bit of doubt about this header~ I have tried everything but it still doesn’t work
$arr = array(
'name' => '我是姓名',
'age' => '18'
);
$json_string = json_encode($arr);
echo ($json_string);//The output here is the json string~~
?>
On the mobile phone, an error is reported~But the browser can obtain the data normallyBut it is not working properlyPlease solve it~I just want to get the json data in php~~
It should be that the front-end and back-end formats do not match. Try removing ajax dataType: "json". The obtained data is String, and then convert it to json in the front-end.