数据不附加jquery

时间:2014-01-03 18:42:12

标签: jquery ajax json

我通过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但没有任何数据(即空)

1 个答案:

答案 0 :(得分:0)

您在成功回调中引用了this,但它应该是data。我相信在此上下文中this引用了全局对象,因此this.titlethis.body未定义,因此是空白值。

相关问题