2011-07-13 45 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