JQuery Post数据处理

JQuery Post Data Manipulation

本文关键字:数据处理 Post JQuery      更新时间:2023-09-26

我试图从表单提交获得数据。代码如下:

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;
}

现在的问题是我不能把数据转换成我想要的变量。此外,我认为数据是作为字符串而不是数组发送的。以下是用于调试的alert(data)的输出

{"questionName":"测试"、"pollingStatus":"0"、"选项":{"1":"Test7","2":"Test8","3":"Test9","4":"Test10","5":"Test11"},"codeExist":真正}

现在上面jsonencode的输出看起来是正确的。然而,这里的问题是data[0]的输出。

{

所以我认为jsonencode作为字符串返回。我希望能够访问像questionName这样的数据。我该怎么做呢?请帮助如果你可以。

你可以告诉jQuery使用"json"数据类型:

$.post('url', postData, function(returnData) {
  alert(returnData.question);
}, 'json');

看到文档。

正如OhGodWhy在上面发布的那样,您还必须在客户端解析json。可以使用

obj = jQuery.parseJSON(data);