【问题标题】:data not appending with jquery数据未附加 jquery
【发布时间】:2014-01-04 03:33:31
【问题描述】:

我正在通过 ajax 从 url 作为 JSON 接收数据。数据采用 UTF-8 字符(乌尔都语字符)。它们一切正常,如果显示在警告框中,可以很好地看到。但是当它们被附加到使用 jquery 的标记时,标记保持为空。代码如下:

主要部分标签:

<section class="row" id="news_detail">
   <img src="../images/loading.gif" alt="loading" id="loadingimg" class="img-responsive center-block" style="width: 300px; height: auto;" />

</section>   <!--loading img-->

ajax 代码:

$(document).ready(function(){

    var id = getParameterByName('id');

    $.ajax({
        url: 'http://localhost/drupal/get/news',
        type: 'GET',
        dataType: 'json',
        crossDomain: true,
        success: function(data){

            $("#loadingimg").hide();

            if(data.status == true)
            {
                var titletag = $("<p>").addClass("article_title").append(this.title);

                var img = $("<img>").addClass("img-responsive center-block summary-image").attr("src", this.image_url).attr('alt',this.title);

                var p = $("<p>").addClass("article_body").html(this.body);
                   // alert(data.body);

                $(titletag).appendTo($("#news_detail"));
                $(img).appendTo($("#news_detail"));
                $(p).appendTo($("#news_detail"));
            }
            else if(data.status == false)
            {
                alert("The selected article could not be loaded. please try later");
            }

            $("#news_detail").append($("<div>").addClass("clear").html("&nbsp;"));
        },
        error: function()
        {
            $("#loadingimg").hide();
            alert("An error occured while retrieving the article. please try later");
        }
    });

});

如前所述,json 很好,可以在警告框中看到(数据主要是 URDU 语言的 UTF-8 代码)。但是,为了澄清起见,json的结构如下:

{"status": true, "title": "abc", "image_url": "abc.jpg", "body": "text" }

动态标签被附加到 DOM 但没有任何数据(即空)

【问题讨论】:

    标签: jquery ajax json


    【解决方案1】:

    您在成功回调中引用了this,但它应该是data。我相信 this 在这种情况下指的是全局对象,所以 this.titlethis.body 是未定义的,因此是空白的值。

    【讨论】:

    • 谢谢你的回答。有用。复制代码的另一个损失:)。该代码实际上是从另一个返回数组的 ajax 调用中复制的,我正在使用 data.each 函数遍历它,这就是它使用 this 的原因。再次感谢您.. 小事没有想到:)
    猜你喜欢
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2019-07-07
    相关资源
    最近更新 更多