且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用javascript或json从数据集中获取数据

更新时间:2023-09-08 19:20:10

.ajax({
类型:POST,
contentType:application / json; charset = utf-8,
u rl:LoadTest.aspx / GetQuiz,
data:{},
dataType:json,
success:function(data){
// for( var i = 0;我< data.d.length; i ++){
//
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "LoadTest.aspx/GetQuiz", data: "{}", dataType: "json", success: function (data) { // for (var i = 0; i < data.d.length; i++) { //


(#tbDetails)。append(< tr>< td>+ data.d [i] .UserId + < / td>< td>+ data.d [i] .UserName +< / td>< td>+ data.d [i] .Location +< / td>< / TR>中);
//}
alert(data);
},
错误:函数(结果){
alert(result);
}
});
}
("#tbDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>"); // } alert(data); }, error: function (result) { alert(result); } }); }





代码落后:





code behind :

  [WebMethod]
    // Let .net handle the request and response as json automatically.
    public static FetchQuiz[] GetQuiz(FetchQuiz q)
    {
        DataSet ds = new DataSet();
        List<FetchQuiz> details = new List<FetchQuiz>();
        SQLDataAccessHelper sq = new SQLDataAccessHelper();
        string query = "";
        query = "select * from tbl_question_master order by Id";
        ds = sq.ExecuteCommandText(query);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            FetchQuiz quiz = new FetchQuiz();
            quiz.Question = ds.Tables [0].Rows[i]["question"].ToString();
            quiz.Answer = ds.Tables[0].Rows[i]["answer"].ToString();
           // quiz.CorrectAnswer = ds.Tables[0].Rows[i]["UserId"].ToString();
            details.Add(quiz);
        }

        return details.ToArray();
    }
}
public class FetchQuiz
{
    public string Question { get; set; }
    public string Answer { get; set; }
   // public string CorrectAnswer { get; set; }
    //public string Type { get; set; }

}





现在问题是我无法获取数据......我也是不知道如何迭代数据..



我的数据将采用这种格式



问题ID输入问题答案

1 MCQ abc a

1 MCQ abc b

1 MCQ abc c

2 SR xyz a

2 SR xyz b





i希望以这种格式显示div标签中的数据

[问题]

A [Option1]

B [Option2]

C [Option3]



等......



请帮我实现这个目标。



提前致谢



Krunal



Now the problem here is i m not able to get data... also i am not aware how to iterate with data..

my data would be in this format

Question Id Type Question Answer
1 MCQ abc a
1 MCQ abc b
1 MCQ abc c
2 SR xyz a
2 SR xyz b


i want to display data in div tag in this format
[Question]
A [Option1]
B [Option2]
C [Option3]

and so on...

Please help me to achieve this.

Thanks in advance

Krunal


请看下面的链接。基本上它是一个名为knockout的javascript插件。

它将数据绑定到html控件,所以在你的问题中你可以例如do
Look at the link below. Basically it's a javascript plugin called knockout.
it binds data to html controls, so in your problem you can e.g. do
<div databind="foreach: { data: question, as 'question' }"></div>



然后是一些html来呈现每个问题,例如as


then some html to render each question e.g. as

<ul><li>Question 1: blah blah</li></ul>





如果您有一点时间学习它,那么将json数据处理为html是***的选择。 />


来自json的淘汰赛 [ ^ ]