Maison  >  Article  >  développement back-end  >  javascript - 求助!!!关于mui ajax获取不到后台数据

javascript - 求助!!!关于mui ajax获取不到后台数据

WBOY
WBOYoriginal
2016-08-04 09:21:041273parcourir

js代码如下
<script></script>

<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格式啊
            }
        });
    
</code>

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(

<code>'name' => '我是姓名', 
'age' => '18' </code>

);
$json_string = json_encode($arr);
echo ($json_string);//这里输出就是json字符串了啊~~
?>

手机端就是报错~而浏览器到是能正常获取数据是在搞不定了求解~我就是想获取php内的json数据啊~~

回复内容:

js代码如下
<script></script>

<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格式啊
            }
        });
    
</code>

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(

<code>'name' => '我是姓名', 
'age' => '18' </code>

);
$json_string = json_encode($arr);
echo ($json_string);//这里输出就是json字符串了啊~~
?>

手机端就是报错~而浏览器到是能正常获取数据是在搞不定了求解~我就是想获取php内的json数据啊~~

应该是前后台格式不匹配,ajax dataType :"json" 去掉试试,获取的data 为String,在前台再转成json

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn