我試圖讓從提交表單數據。這是代碼。JQuery的郵政數據操作
function codeSubmission() {
$("#questionCodeForm").submit(function() {
$.post("SubmitCode.php", $("#questionCodeForm").serialize()).done(function(data) {
var questionName = data.questionName,
options = data.options,
pollingStatus = data.pollingStatus,
codeExist = data.codeExist;
alert(data);
alert(data[1])
alert(questionName);
alert(options);
if(codeExist == true) {
$("#quizTitle").text("questionName");
for(rowNum=1;rowNum<=5;rowNum++) {
$("#checkbox-"+rowNum).val("Answer1");
$("#checkbox"+rowNum+"label").text("Answer"+rowNum);
}
$("#answerForm").slideDown(500);
} else if(codeExist == false) {
alert("This quiz code is invalid");
}
});
return false;
});
return false;
}
現在的問題是我無法將數據導入到我想要的變量中。此外,我認爲數據是作爲一個字符串而不是數組發送的。這裏是出於調試目的的警報(數據)的輸出
{「questionName」:「Test」,「pollingStatus」:「0」,「options」:{「1」:「Test7」,「2 「:」 Test8" , 「3」: 「Test9」, 「4」: 「Test10」, 「5」: 「TEST11」}, 「codeExist」:真}
現在從jsonencode輸出上述似乎是對的。但是看到這裏的問題是數據[0]的輸出。
{
所以我覺得jsonencode將返回一個字符串。我想要做的就是訪問像questionName這樣的數據。我該怎麼做呢?如果可以的話請幫忙。
嗯,當然它返回一個字符串,JSON是將代表一個對象...字符串格式的符號。這裏的問題是,'$。員額()'不解碼發送的JSON對象回來,所以你需要'$ .parseJSON(數據)'來得到你想要的。 – Ohgodwhy 2013-02-26 19:58:29
所以而不是$ .post我寫$ .parseJson? – 2013-02-26 20:07:29
您可以使用以下任一提到答案,作爲傳遞'json'作爲一個字符串$。員額方法,會告訴它使用'$ .parseJSON()'的'returnData',或者你可以做,'returnData = $ .parseJSON(returnData);' – Ohgodwhy 2013-02-26 20:12:18