2016-05-17 48 views
0

我最近更新了我的PHP和用於解析我的PHP到Ajax的代碼已停止工作。我改變了我的PHP使用PDO,不得不使用jsoncallback簡單JSON編碼改變我return語句:AJAX解析錯誤

<?php 
    header('Content-type: application/json'); 


    $con = new PDO('mysql:host=localhost;dbname=db;charset=utf8mb4;''root', 'pass', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 


    $sql = "SELECT idlocations, name, latitude, longitude FROM `locations`"; 
    $con->query("SET NAMES utf8"); 
    $result = $con->query($sql); 


    $records = array(); 

    if($result !== false) 
    { 
     $records = $result->fetchAll(PDO::FETCH_ASSOC); 

     print_r($records); 
     //$_GET['jsoncallback'] . '(' . json_encode($records) . ');'; 

     echo json_encode($records); 
    } 
    else 
    { 
     print_r($con->errorInfo()); 
    } 
    ?> 

上面的代碼將返回如下:

Array 
    (
     [0] => Array 
     (
       [idlocations] => 1 
       [name] => BierMarkt 
       [latitude] => -79.3708 
       [longitude] => 43.6473 
     ) 

     [1] => Array 
     (
       [idlocations] => 2 
       [name] => jacks 
       [latitude] => -79.4200 
       [longitude] => 43.6555 
     ) 

) 
     [{"idlocations":"1","name":"BierMarkt","latitude":"-79.3708","longitude":"43.6473"},{"idlocations":"2","name":"jacks","latitude":"-79.4200","longitude":"43.6555"}] 

然而,當我嘗試使用AJAX從JavaScript調用時,收到以下錯誤:

[對象的對象]:parsererrorSyntaxError:在JSON意外標記阿在位置0

JS :

function populateLocations() 
    { 
     console.log("inside populate"); 
     //Get other locations from the DB 
     $.ajax({ 
       url:'http://localhost/VibeSetter/services/getlocations.php', 
       type: 'POST', 
       dataType: "json", 
       timeout: 5000, 
     success: function(data, status) 
     { 
      console.log("inside success"); 
      $.each(data, function(i,item) 
      { 
       console.log("inside index"); 
       console.log("item.longitude"); 
       //pass to function to fill array 
       populateLocationsArray(i+1,item.name, item.longitude, item.latitude); 

      }); 
     }, 

     error: function(e, ts, et) { console.log(e + " : " + ts + et) } 
    }); 
    } 

     function populateLocationsArray(i, name, long, lat) 
     { 
      locations[i] = new Array(); 
      locations[i][0] = name; 
      locations[i][1] = long; 
      locations[i][2] = lat; 
     } 

有人可以幫我理解爲什麼我不能正確地解析從我的PHP輸出?我嘗試了很多不同的方法,包括刪除'dataType:「json」'並在結果中使用JSON.parse。

在此先感謝

+0

刪除'的print_r($記錄);'爲創建無效的JSON。你只想輸出'echo json_encode($ records);' – Sean

回答

0

有在你的代碼少的錯誤,如:

  • 請參見下面的行,

    $con = new PDO('mysql:host=localhost;dbname=db;charset=utf8mb4;''root', 'pass', ... 
                       ^missing comma 
    

    它應該是,

    $con = new PDO('mysql:host=localhost;dbname=db;charset=utf8mb4;','root', 'pass', ... 
    
  • 從你的代碼中刪除這條語句print_r($records);

  • 看到這個說法,

    print_r($con->errorInfo()); 
    

    既然你期待一個JSON對象爲來自服務器的響應,您必須將錯誤信息陣列編碼。它應該是,

    echo json_encode($con->errorInfo()); 
    

所以,你的代碼應該是這樣的:

<?php 
    header('Content-type: application/json'); 

    $con = new PDO('mysql:host=localhost;dbname=db;charset=utf8mb4;','root','pass', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 

    $sql = "SELECT idlocations, name, latitude, longitude FROM `locations`"; 
    $con->query("SET NAMES utf8"); 
    $result = $con->query($sql); 


    $records = array(); 
    if($result !== false){ 
     $records = $result->fetchAll(PDO::FETCH_ASSOC); 
     echo json_encode($records); 
    }else{ 
     echo json_encode($con->errorInfo()); 
    } 
?> 
+0

嗨我添加了你的代碼,並且發生了同樣的錯誤,即使在打印$ con> errorinfo();現在返回的錯誤是:[object Object]:parsererrorError:jQuery1113014746108321211326_1463615437202不叫 – Mark

+0

@Mark,單獨運行php代碼是單獨的頁面,看看你得到了什麼錯誤。另外,在你的jQuery腳本中,你還沒有在任何地方定義'locations',所以語句location [i] = new Array();'會拋出這個錯誤,* ReferenceError:locations沒有被定義。 –