2013-05-10 77 views
0

bellow腳本在我的本地主機上運行,​​但不在我的網站上運行。 我只是在jqueryMobile列表格式測試簡單的JSON腳本 任何想法是什麼錯?加載ajax JSON失敗

感謝

HTML頁面點擊呼叫的Ajax功能:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <link rel="stylesheet" href="protected/jQuery/jquery.mobile.structure-1.0.1.min.css" /> 
    <link rel="stylesheet" href="protected/themes/Green/red_theme.min.css" /> 
<script src="news_services/js/jquery.js"></script> 
<script src="news_services/js/jquery.mobile-1.0rc1.min.js"></script> 
<script type="text/javascript" src="protected/jsnM.js"> </script> 
</head> 
<body > 
<div data-role="page" id="home" > 
    <div data-role="header" > 
     <h1>JSON testing</h1> 
     <div data-role="controlgroup" data-type="horizontal"></div> 
    </div> 
    <div data-role="content"> 
     <p> Json testing</p> 
    </div> 
    <div data-role="footer"> 
     <a href="#getAll_JSON" onClick="jsn1()" data-role="button" data-inline="true" data-icon="info">call JSON</a> 
    </div> 
</div> 
<div data-role="page" id="getAll_JSON"> 
    <div data-role="header" data-position="fixed" > 
     <h1>Json format</h1> 
     <div data-type="horizontal" > 
      <a href="#home" data-role="button" data-inline="true" data-icon="home">Home</a> 
     </div> 
    </div> 
    <div data-role="content"> 
     <ul id="sitesList" data-role="listview" data-filter="true" data-split-icon="gear" > 
     </ul> 
    </div> 
    <div data-role="footer" data-position="fixed"></div> 
</div> 
<div id="detailsPage" data-role="page" > 
    <div data-role="header"> 
     <h1>info</h1> 
     <div data-type="horizontal" > 
      <a href="#home" data-role="button" data-inline="true" data-icon="home">Home</a> 
     </div> 
    </div> 
    <div data-role="content"> 
     <div id="sitesDetail" > 
     </div> 
    </div> 
</div> 
</body> 
</html> 

調用Ajax的位置:

var areaID=0; 

function jsn1(val) 
{ 
var url_Category="http://gonorth.co.il/protected/indexJSON.php?"; 

$.ajax({ 
    url: url_Category, 
    type: "GET", 
    data: 'No='+val+"&area="+areaID, 
    dataType: "json", 
    cache: false, 
    error: function() { 
     alert('loading Ajax failure'); 
     } , 
    onFailure: function() { 
     alert('Ajax Failure'); 
    } , 
    statusCode: { 
     404: function() { 
      alert("missing info"); 
     } 
    }, 
    success: function(result) { 
      $('#sitesList li').remove(); 
     $.each(result.sites,function(index,dat){ 
      $("#sitesList").append(
       '<li>'+ 
       '<img src="images/'+dat.coupon_img+'" width=80/>' + 
       '<p >Name: '+dat.coupon_name+'</p>'+ 
       '<p >info: '+dat.street+'</p>'+ 
       '<p >address:'+dat.coupon_tmp+'</p>'+ 
       '</li>' 
      ); 
     }); 

     $('#sitesList').listview('refresh');   
    } 
}); 
} 

PHP結果:

<?php 
    $arr = array(); 
     $arr[] =[ 
       "coupon_id" => '1', 
       "coupon_img" => '1.jpg', 
       "street" => 'long text', 
       "coupon_tmp" => 'Address', 
       "coupon_name" => 'Name' 
       ]; 
echo '{"sites":'.json_encode($arr).'}'; 
?> 
+0

確實不以某種特殊的方式工作? – 2013-05-10 09:11:03

+0

在瀏覽器的控制檯中是否存在任何錯誤消息? Ist位於與JSON資源相同的域中的HTML(您可能會遇到跨域問題)。 – Florian 2013-05-10 09:16:30

+0

錯誤消息是Ajax錯誤:function(){ alert('loading Ajax failure'); – user1377921 2013-05-10 09:48:46

回答

0

當我訪問此鏈接:http://gonorth.co.il/protected/indexJSON.php,它看起來像您的JSON通過您的Web服務器通過PHP解析器提供。您應該查看Web服務器的設置,並瞭解如何設置JSON文件的內容類型爲application/json。該電流響應下來了conten型text/html,因爲它試圖通過PHP解析器解析和解析器扼流圈的語法,或類似的東西,然後服務器呈現一個HTML錯誤頁面這是最有可能的。

+0

運行可能是錯的: – user1377921 2013-05-10 09:50:19

+0

你認爲這是錯誤的PHP JSON編碼。在語法中可能是錯誤的:echo'{「sites」:'。json_encode($ arr)。'}'; – user1377921 2013-05-10 09:51:28

+0

我不認爲這是「Web服務器設置」的問題,因爲我在jQuery的乳寧JSON和它工作正常,什麼是錯的我的Ajax設置我敢肯定 – user1377921 2013-05-10 11:35:13

0

感謝所有的答案。 問題,是PHP數組 可能不同的PHP版本我Loclahost和serevr 下一個PHP數組在我的本地workedd但沒;噸先後在服務器

<?php 
$arr = array(); 
    $arr[] =[ 
      "coupon_id" => '1', 
      "coupon_img" => '1.jpg', 
      "street" => 'long text', 
      "coupon_tmp" => 'Address', 
      "coupon_name" => 'Name' 
      ]; 
echo '{"sites":'.json_encode($arr).'}'; 
?> 

我改成了

<?php 
    $arr[] =array(
      "coupon_id" => '1', 
      "coupon_img" => '1.jpg', 
      "street" => 'long text', 
      "coupon_tmp" => 'Address', 
      "coupon_name" => 'Name' 
      ); 
echo '{"sites":'.json_encode($arr).'}'; 
?> 

,並在服務器工作以及

0
<?php 
    $arr = array(); 
     $arr[] =[ 
       "coupon_id" => '1', 
       "coupon_img" => '1.jpg', 
       "street" => 'long text', 
       "coupon_tmp" => 'Address', 
       "coupon_name" => 'Name' 
       ]; 
     $res['sites']=$arr; 
     echo json_encode($res); 
?>