【发布时间】: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(" "));
},
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 但没有任何数据(即空)
【问题讨论】: