2013-10-25 38 views
1

我在Chrome和Opera上遇到JSON問題好幾天,我設法解決了一些錯誤,但現在我陷入困境,找不到出路。我在PHP中創建了一些數組,並使用JSON對其進行編碼,並將其發送到JavaScript文件,然後再進一步處理它。在Firefox中一切正常,但在Chrome和Opera $.post()函數永遠不會被調用。當我需要接收JSON數據時,我認爲它停止工作。你的幫助非常需要。在Chrome和Opera中無法接收來自PHP的json數據

這是我的javascript函數:

var updateGroup = function(id){ 

    $("#unshareTable").html(""); 
    passID = id; 
    $.post("getData.php", {'id' : id}, function(obj){ 
     globalColor = obj.color; 
     $("#updateGroupTitle").val(obj.title); 
     $("#updateColorpickerHolder").css("background-color", obj.color); 
     $("#updateColorHex").html(obj.color); 
     var color2 = lighterColor(obj.color, .4); 
     $("#updateColorHex2").html(color2); 
     $("#updateGroupDescription").html(obj.description); 
     $("#unshareTable").append("<tr id='firstRowUnshare'><td>Username</td><td>Option</td></tr>"); 
     $.post("getUnshareData.php", {'title' : obj.title}, function(obj1){ 
      var length = obj1.length; 

      for(var i = 0; i < length; i++){ 
       $("#unshareTable").append("<tr><td class='unshareUsername'>"+obj1[i].username+"</td><td><label class='unshareOneButton'>Unshare</label></td></tr>"); 
      } 
     }, "json"); 
    }, "json"); 
    $(".updateGroupDiv").fadeIn(300); 
} 

和簡單的PHP腳本

<?php 
    $id = clean($_POST['id']); 

    $query = $mysqli->query("select * from groups where id_group = '$id'"); 
    $data = $query->fetch_assoc(); 
    echo json_encode($data); 

?> 

而且在Chrome中我得到這些錯誤:

Uncaught TypeError: Cannot call method 'getURL' of undefined 

Uncaught TypeError: Cannot read property 'onRequest' of undefined 

回答

2

在你的PHP,添加以下到第一行(在您打開標籤後)

header("Content-Type: text/javascript; charset=utf-8"); 

它指定

+0

太感謝你了,你救了我的反應是JSON。爲了記錄,我不得不添加session_start();在我的PHP文件中,不知道爲什麼,但沒有它就無法工作。 – Alen