首頁  >  文章  >  後端開發  >  javascript - 求救! ! !關於mui ajax取得不到後台數據

javascript - 求救! ! !關於mui ajax取得不到後台數據

WBOY
WBOY原創
2016-08-04 09:21:041272瀏覽

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn