我正在使用codeigniter框架來開發我的網站。目前,我使用jQuery發送一個AJAX請求以從服務器獲取數據。我試過兩種方法,一種是數據在PHP關聯數組中,另一種是在JSON對象中。請看到我從AJAX請求返回的數據:如何在codeigniter中獲取json對象的值
{
"o": [
{
"q_id": "83",
"t_id": "4",
"question": "jjjjs.jfdaskldjf",
"option1": "jjjjasdfasdf",
"option2": "jjj",
"option3": "lll",
"option4": "lll",
"answer": "lll",
"marks": "22"
},
{
"q_id": "84",
"t_id": "4",
"question": "This is testing",
"option1": "2",
"option2": "7",
"option3": "8",
"option4": "9",
"answer": "2",
"marks": "2"
},
{
"q_id": "85",
"t_id": "4",
"question": "hello this is another test",
"option1": "a",
"option2": "b",
"option3": "c",
"option4": "d",
"answer": "a",
"marks": "2"
},
{
"q_id": "86",
"t_id": "4",
"question": "another test",
"option1": "8",
"option2": "9",
"option3": "0",
"option4": "1",
"answer": "1",
"marks": "2"
},
{
"q_id": "87",
"t_id": "4",
"question": "last question ",
"option1": "z",
"option2": "x",
"option3": "c",
"option4": "v",
"answer": "c",
"marks": "2"
}
]
}
我訪問它:
alert(data["o"][0]);
但出現的錯誤是:
Uncaught TypeError: Cannot read property '0' of undefined
任何人都可以給我一些關於如何獲取值或將對象存儲在數組中的建議。
你可以發佈讓服務器發出ajax請求的jQuery代碼嗎? – james246 2013-02-09 17:10:03