2013-10-13 29 views
0

問題:當我加載頁面時,get_user_list();不起作用。它不附加任何返回的編碼JSON。該jQuery.parseJSON$ .parseJSON OR jQuery.getJSON工作。jQuery.parseJSON不附加任何值

我對正在發生的事情的猜測: 也許我應該試試這個在線看看它是否真的有效。我相當肯定,這是parseJSON的一個不工作。

有什麼我試圖解決我的問題?

  1. 我試着使用$ .getJSON但沒有奏效,因爲它給了我一個403 錯誤。
  2. 我嘗試下載jQuery庫,並在本地加載它,而不是從Google的庫中獲取它。
  3. 將數據插入數據庫時​​使用addslashes()。

這是我正在使用的AJAX。

$(document).ready(
    function() { 
     get_user_list(); 
    }); 

function get_user_list() { 
    $.post("ajax.php", { 
     task: "get_user_list" 
    }, 
    function(data) { 
     $('#responseText').val(data); //This ACTUALLY WORKS 
     var people = jQuery.parseJSON(data); //Problem here 
     for (var i in people) { 
      var opt = "<option value='" + people[i].id + "'>" + people[i].first_name + "</option>"; 
      $('#subsriber').append(opt); 
     } 
    }); 
} 

這是HTML:

<select id="subscriber"> 
    <option value="0" selected>Select Subscriber</option> 
</select> 

這是一塊編碼的JSON的將被返回的 「數據」:

[{"id":"1","first_name":"Mother","last_name":"Teresa"},{"id":"2","first_name":"Martin","last_name":"Lutherking"}] 

如果任何人都可以引導我進入正確的方向,這將是偉大的。 謝謝。

回答

0

我的猜測是您購買的已經被解析的數據,所以儘量

function get_user_list() { 
    $.post("ajax.php", { 
     task: "get_user_list" 
    }, function (people) { 
     $('#responseText').val(people); //This ACTUALLY WORKS 

     $.each(people, function (idx, person) { 
      var opt = "<option value='" + person.id + "'>" + person.first_name + "</option>"; 
      $('#subsriber').append(opt); 
     }) 
    }, 'json').fail(function (jqXhr, status, error) { 
     alert(status + ':' + error + ':' + jqXhr.responseText); 
    }); 
} 
+0

試了一下。舊的和您的修訂版本的代碼。我現在除了[對象對象],[對象對象]以外沒有其他任何東西。也許我做錯了?爲什麼人們被傳入功能? –

+0

@TiffanyLowe嘗試更新 –