【问题标题】:How to use json returned by an ajax call?如何使用ajax调用返回的json?
【发布时间】:2014-01-08 14:29:07
【问题描述】:

这是我用来创建无限分页的脚本。

var pageNum = 1; // The latest page loaded
    var hasNextPage = true; // Indicates whether to expect another page after this one

// loadOnScroll handler
var loadOnScroll = function() {
   // If the current scroll position is past out cutoff point...
    if ($(window).scrollTop() > $(document).height() - ($(window).height()*2)) {
        // temporarily unhook the scroll event watcher so we don't call a bunch of times in a row
        $(window).unbind(); 
        // execute the load function below that will visit the JSON feed and stuff data into the HTML
        loadItems();
    }
};

var loadItems = function() {
    // If the next page doesn't exist, just quit now 
    if (hasNextPage === false) {
        return false
    }
    // Update the page number
    pageNum = pageNum + 1;
    // Configure the url we're about to hit
    var url = document.getElementsByName('page-url')[0].value
    $.ajax({
        url: url,
        data: {page_number: pageNum},
        dataType: 'json',
        success: function(data) {
            // Update global next page variable
            hasNextPage = true;//.hasNext;
            // Loop through all items
            for (i in data) {
              $.each(data, function(index, element) {
                $('.fest-content-event:last').append($('<div>', {
                  text: element.name
                }));
              });
              }
        },
        error: function(data) {
            // When I get a 400 back, fail safely
            hasNextPage = false
        },
        complete: function(data, textStatus){
            // Turn the scroll monitor back on
            $(window).bind('scroll', loadOnScroll);
        }
    });
};
        $(window).bind('scroll', loadOnScroll);

ajax 调用返回 json 如下:

"[{\"pk\": 1, \"model\": \"f.event\", \"fields\": {\"description\": \"The authority_name is specified when one declares a Content Provider in Andr and points to the Content as an email message\\nstored by the Content Provider. Thus, a URI into a Content Provide\", \"title\": \"\", \"rank\": \"0\", \"f\": 1, \"user\": 1, \"pub_date\": \"2014-01-07T05:42:28.258Z\"}}]"

我想将返回的 json 附加到 html div 中,如下所示:

     {% for event in events %}
      <div class="f-content">
      <div class="f-content-event">
        {% if event.description %}
          {{ event.description|linebreaksbr }}
        {% endif %} 
  </div><!-- .f-content-event-->
  </div><!-- .f-content ends -->
      {% endfor %}

我该怎么做?

【问题讨论】:

    标签: javascript jquery ajax django json


    【解决方案1】:

    只需将此 json 字符串加载到 dict 对象中并在模板上呈现。

    这是一个小例子..

    >>> import json
    >>> s = '{"foo": 6, "bar": [1, 2, 3]}'
    >>> d = json.loads(s)
    >>> print d
    {u'foo': 6, u'bar': [1, 2, 3]}
    

    现在你可以在你的模板中使用这个 dict 对象了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2015-03-23
      • 1970-01-01
      • 2011-09-21
      • 2012-07-11
      • 1970-01-01
      相关资源
      最近更新 更多