• 推荐
  • 评论
  • 收藏

使用JQuery遍历解析由.ashx返回的json的值

2022-11-15    1109次浏览

使用JQuery遍历解析由.ashx返回的json的值

在网上查找了很多次,都没有发现真正能解决这个问题的。不过后来找到一个方法,试了试还不错,不过有些地方不是太明白,就是.ashx返回的json的字段为何只能使用小写,使用大写就提示没有定义。遍历值之后,动态填充的ul里作为文章列表。如下图:

jquery code:

/// <reference path="http://www.cnblogs.com/js/jquery-1.4.2.js" />
2
 
3
$(document).ready(function () {
4
    $(window.document).ready(function () {
5
 
6
        //搜索
7
        $("#txtsearch").keydown(function (event) {
8
            if (event.keyCode == 13) {
9
                $("#ulnew").html("");
10
                //提交关键词进行搜索
11
                $.ajax({
12
                    url: "askCommon.ashx?action=s",
13
                    type: "post",
14
                    data: { "key": $("#txtsearch").val() }, //提交表单,相当于CheckCorpID.ashx?ID=XXX
15
                    datatype: "json",
16
                    success: function (data) {
17
 
18
                        var json = eval(data);
19
                        var htmlStr = "";
20
                        $.each(json, function (i, n) {
21
                            htmlStr += " <li class='cle'><span class='ans-num'>" + n['replaycount'] + "回答</span> ";
22
                            htmlStr += "  <span class='price'>" + n['questionscore'] + "</span> ";
23
                            htmlStr += "  <span class='title'>";
24
                            htmlStr += "        <a  href='view" + n['questionid'] + ".htm' target='_blank'>" + n['title'] + "</a>";
25
                            htmlStr += "     </span> ";
26
                            htmlStr += " </li>";
27
                        });
28
                        $("#ulnew").html(htmlStr);
29
 
30
                    }
31
                });
32
            }
33
        })
34
 
35
    });
36
});

原文地址:https://www.cnblogs.com/Leo_wl/p/2000640.html