js代码如下
<script>
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>
php代码如下
<?php
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');
//这个header头有点疑问~所有都试了一遍还是没用
$arr = array(
'name' => '我是姓名',
'age' => '18'
);
$json_string = json_encode($arr);
echo ($json_string);//这里输出就是json字符串了啊~~
?>
手机端就是报错~而浏览器到是能正常获取数据是在搞不定了求解~我就是想获取php内的json数据啊~~
应该是前后台格式不匹配,ajax dataType :"json" 去掉试试,获取的data 为String,在前台再转成json