0
我在我的主html頁面上有一個getJSON函數。 ID是一個整數,我試圖傳遞給PHP(JSON編碼)用作我的常見問題解答陣列中的一個鍵。我想知道如何引用傳入php的字符串(1,2或3)來使用它。如何在json中引用getJSON的數據參數?
的getJSON代碼:
$.getJSON("faqs.php", id, function(data){
//$.getJSON("faqs.php?topic=field", function(data) {
if(data == "") {
$("<div>", {class: "list-group-item", text: "Please add FAQs."}).appendTo($("#topic-container"));
}
$.each(data, function(faqId, faq){
$("<div>", {id: "faq" + faqId, class: "list-group-item", text: faq}).appendTo($("#topic-container"));
});
});
faqs.php
header('Content-Type: application/json; charset=utf-8');
{"faqs":
{"1":[
{
"question": "What is the best musical instrument in the world?",
"answer": "The English concertina"
},
{
"question": "How many double bass players does it take to change a light bulb?",
"answer": "None, the piano player can do that with his left hand."
}
],
"2":[
{
"question": "Why do programmers confuse halloween and christmas?",
"answer": "Because Oct 31 = Dec 25."
}
],
"3":[
{
"question": "Should I eat more pizza?",
"answer": "Yes. Always."
}
]
}
}
如果您沒有注意到,我已經爲您的問題發佈了一個答案! – EhsanT