2011-07-13 51 views
0

我很困惑,爲什麼警報不響應json響應。反應正在進入螢火蟲。當我使用php4.4.7時,這是工作正常,但後來升級到php5.3.5,現在產生這個錯誤。或者可能是我的錯誤。有人可以檢查我的代碼,看看我哪裏出錯了嗎?如果您需要更多代碼,請告訴我。非常感謝警報不響應json響應

http://jsfiddle.net/QQtVv/

代碼在這裏爲每個請求:

function test(com,grid) 
{ 
    if (com=='Delete') 
     { 
      if($('.trSelected',grid).length>0){ 
      if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){ 
      var items = $('.trSelected',grid); 
      var itemlist =''; 
      for(i=0;i<items.length;i++){ 
       itemlist+= items[i].id.substr(3)+","; 
      } 
      $.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "fileinrptdelete.php", 
       data: "items="+itemlist, 
       success: function(data){ 
        alert("You have successfully deleted:"+"\n\n"+"Customer: "+data.customer+"\n"+"name: "+data.ref+"\n"+"boxref: "+data.boxref); 
       $("#flex1").flexReload(); 
       } 
      }); 
      } 
      } else { 
       alert('You have to select a row to delete.'); 
      } 
       } 


    } 

// this is from the file fileinrptdelete.php 

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pragma: no-cache"); 
header("Content-type: text/x-json"); 
$json = ""; 
$json .= "{\n"; 
$json .= "name: '".$ref."',\n"; 
$json .= "company: '".$customer."',\n"; 
$json .= "boxref: '".$boxref."',\n"; 
$json .= "total: $total\n"; 
$json .= "}\n"; 
echo $json; 
+3

實時鏈接對於一個問題來說是一個很好的*附件*,但也總是在問題*中發佈相關的代碼*。兩個原因。 1.人們不應該遵循鏈接來幫助你。 2. StackOverflow不僅適用於您,而且適用於將來也有類似問題的其他人。外部鏈接可以被移動,修改,刪除等。通過確保相關代碼在問題中,我們確保問題(及其答案)在合理的時間段內保持有用。 –

回答

3

的JSON是invalid(與錯誤的內容類型服務的,它應該是應用/ JSON)。

不要手工製作它。使用a library

您還試圖使用您在PHP中使用的變量名讀取數據,而不是使用您應用於JSON中的鍵的名稱。

+0

我現在正在收到警報,但內容未定義。是echo $ json;在php5中正確調用? – bollo

+0

我以爲我在讀json鍵:$ json。=「\」name \「:\」「。$ ref。」\「,\ n」; 「+」客戶:「+ data.company +」\ n「+」名稱:「+ data.ref +」\ n「+」boxref :「+ data.boxref);如果這不正確,請粘貼一個樣本進行查看。謝謝 – bollo

+0

好吧,我現在看看我做了什麼。我叫ref而不是名字。 – bollo

1

一個有效的JSON屬性和值必須用雙引號「名」括:「值」比它看起來還好我 等。

+0

它們用雙引號括起來? $ json。=「文件:'」。$ custref。「',\ n」; – bollo

+0

沒有那些是PHP的報價,而不是json的報價。你需要它像$ json。=「\」File \「:\」「。$ custref。」\「,\ n」; – TheBrain

+0

好的我現在明白了。謝謝 – bollo