2011-12-07 34 views
0

我一直在努力獲取$ .ajax數據編譯通過使用我寫的PHP類。數據出來是這樣的:自定義字符串 - 缺少:屬性列表後

{ 
0: [ 
    { Status : "example status 2" 
    , Contact ID Number : "1" 
    , Date Entered : "2011-06-15" 
    , Date Assigned : "2011-06-15" 
    , Date Last Modified : "2011-06-15" 
    , Deceased Date : "" 
    , Linked To Registrant 1 : "Leona Brown" 
    , Relationship 1 : "Father" 
    , Authorization 1 : "1" 
    , Linked To Registrant 2 : "Henry Sale" 
    , Relationship 2 : "Husband" 
    , Authorization 2 : "1" 
    , Location : "Hamilton Harron Funeral Home" 
    , Sales Manager : "Dan Thompson" 
    , Counselor : "Mike Brown" 
    , Registration Number : "1" 
    , Registration Date : "2001-05-11" 
    , Program 1 Number : "2" 
    , Program 1 Date : "2003-11-05" 
    , Program 1 Name : "Non Registrant Test Program - Advantage" 
    , Program 2 Number : "--" 
    , Program 2 Date : "--" 
    , Program 2 Name : "--" 
    , First Name : "Onelia" 
    , Last Name : "Garcy" 
    , Address Location 1 : "home" 
    , Address 1 : "" 
    , City 1 : "Houston" 
    , State/Province 1 : "Texas" 
    , Zip/Postal Code 1 : "77019" 
    , Home Phone : "--" 
    , Work Phone : "--" 
    , Mobile Phone : "--" 
    , Pager : "--" 
    , Other Phone : "--" 
    , Email : "" 
    , Sales Type : "Pre-Need" 
    , Note 1 : "This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. " 
    } 
] 
, 1: [ 
    { Status : "" 
    , Contact ID Number : "2" 
    , Date Entered : "2011-07-02" 
    , Date Assigned : "2011-07-02" 
    , Date Last Modified : "2011-07-02" 
    , Deceased Date : "" 
    , Linked To Registrant 1 : "--" 
    , Relationship 1 : "--" 
    , Authorization 1 : "--" 
    , Linked To Registrant 2 : "--" 
    , Relationship 2 : "--" 
    , Authorization 2 : "--" 
    , Location : "Floral Haven Funeral Home " 
    , Sales Manager : "Jayson Gordon" 
    , Counselor : "Henry Murdoc" 
    , Registration Number : "3" 
    , Registration Date : "2003-12-02" 
    , Program 1 Number : "4" 
    , Program 1 Date : "2004-03-05" 
    , Program 1 Name : "Non Registrant Test Program - Advantage" 
    , Program 2 Number : "--" 
    , Program 2 Date : "--" 
    , Program 2 Name : "--" 
    , First Name : "Leo" 
    , Last Name : "Brown" 
    , Address Location 1 : "home" 
    , Address 1 : "5330 Ridge Rd " 
    , City 1 : "Burlington" 
    , State/Province 1 : "Ontario" 
    , Zip/Postal Code 1 : "LVL7N9" 
    , Home Phone : "9053919735" 
    , Work Phone : "--" 
    , Mobile Phone : "9057303737" 
    , Pager : "--" 
    , Other Phone : "--" 
    , Email : "[email protected]" 
    , Sales Type : "PN to AN" 
    , Note 1 : "This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. This is Note 5. " 
    } 
] 
, 2: [ 
    { Status : "example1 status" 
    , Contact ID Number : "3" 
    , Date Entered : "2011-09-05" 
    , Date Assigned : "2011-09-05" 
    , Date Last Modified : "2011-09-05" 
    , Deceased Date : "" 
    , Linked To Registrant 1 : "Onelia Gary" 
    , Relationship 1 : "Wife" 
    , Authorization 1 : "1" 
    , Linked To Registrant 2 : "--" 
    , Relationship 2 : "--" 
    , Authorization 2 : "--" 
    , Location : "First Memorial Funeral Services" 
    , Sales Manager : "Dan Thompson", Counselor : "Steve Brown" 
    , Registration Number : "5" 
    , Registration Date : "2010-11-11" 
    , Program 1 Number : "7" 
    , Program 1 Date : "2011-08-19" 
    , Program 1 Name : "Non Registrant Test Program - Dignity" 
    , Program 2 Number : "6" 
    , Program 2 Date : "2011-07-03" 
    , Program 2 Name : "Non Registrant Test Program - Advantage" 
    , First Name : "Henry" 
    , Last Name : "Murdoc" 
    , Address Location 1 : "company" 
    , Address 1 : "9055 Harvy Rd. 4" 
    , City 1 : "Burlington" 
    , State/Province 1 : "Ontario" 
    , Zip/Postal Code 1 : "L7L5Z7" 
    , Home Phone : "9055679602" 
    , Work Phone : "9057455209" 
    , Mobile Phone : "--" 
    , Pager : "--" 
    , Other Phone : "--" 
    , Email : "[email protected]" 
    , Sales Type : "At-Need" 
    , Note 1 : "This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. This is Note 6. " 
    } 
    ] 
} 

螢火報告這個錯誤:

missing : after property id .../view_contacts_search_1.php# Line 188

實例化一個變量時,它會失敗:

<script type='text/javascript' > 
     $(document).ready(function(){ 
       var data = <?php echo($data)?>; 

你能找出其中缺失的元素是什麼/ ?我一直試圖弄清楚這幾個小時,這讓我很難過。剛接觸javascipt和jquery也沒有幫助:)

回答

2

您的屬性名稱是數字 - 這是無效的JavaScript。嘗試重命名頂級屬性或使它們成爲字符串。

這些都是有效的替代品:

{ 
    _0: [ ... 

{ 
    number0: [ ... 

{ 
    '0': [ ... 

但說實話它始終是最好做引用的字符串,以避免類似問題的屬性名稱。

+0

我可以將數字屬性名稱轉換爲字符串,它會起作用嗎? – ServerStorm

+2

是的,然後其他人的_vast多數是多字字符串,不包含引號。 –

+0

好,現在就試着解決這個問題。將看看這是否也是問題的一部分?謝謝! – ServerStorm

0

這將更好地工作:

{ 
"0": [ 
    { "Status" : "example status 2" 
    , "Contact ID Number" : "1" 
    , "Date Entered" : "2011-06-15" 
    , ... 

使用PHP的內置JSON的轉換功能,這將節省您的痛苦。

+0

PHP json_encode不能正確編碼數據。我試過使用它,但是它將[Object object,Object object,Object object]作爲一個字符串。我知道這是一個字符串,因爲當運行json_decode時,結果變量是字符串類型。無賴:( – ServerStorm

+1

)必須有一個原因,爲什麼json_encode不起作用,編寫你自己的json序列化程序不是解決這個問題。 – buddhabrot

+0

是的我相信你是對的;雖然我現在有我的json_encode工作和PHP不?! – ServerStorm