我有这个ajax功能
$.ajax({
url: requestUrl,
type: 'get',
dataType: 'html',
beforeSend: function() {
},
complete: function() {
},
success: function(html) {
var tab_item = document.getElementById(tab);
try{
tab_item.innerHTML = html;
}catch(e){
tab_item.innerText = html;
}
}
});
}
}
使用tab和requestUrl vars在ajax之前设置。 回复是
<p>&</p>
<p>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</p>
<div style="font-family: Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: rgb(255, 255, 255); ">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris at.</p>
</div>
<p>&</p>
它放在div中,看起来像这样
<p> </p> <p> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> </p> <div style="font-family: Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: rgb(255, 255, 255); "> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris at.</p> </div> <p> </p>
我使用$('#'+ tab).html(html)和$('#'+ tab).text(html)而不是try-catch,但它们要么没有结果,要么结果是相同。我也尝试了json dataType,但我遇到了类似的问题。
我想念什么?我应该使用不同的DOM元素吗?
答案 0 :(得分:1)
您的服务器似乎正在向您发送html编码的响应。我认为你应该在将响应插入选项卡之前对其进行html解码。
您可以使用javascript html解码器或使用此帖子中的技巧来实现:
How to decode HTML entities using jQuery?
基本上是:
var decoded = $("<div/>").html(encodedStr).text();