如果返回結果值爲<script>alert('test');</script>
,是否可以在我的div標記中正確打印<script>alert('test');</script>
值?轉義JQuery AJAX結果並保留原始值
$.ajax({
url:'${pageContext.request.contextPath}/node/get/2.do',
method:'GET',
data:{name:nodeName, parentId: parentId},
dataType:'json',
success:function (result, textStatus, xhr){
$('#div1').html(result);
}
error: function(xhr, textStatus, errorThrown){
$('#div1').html('error'); }
}))
UPDATE:
對不起,這是一個壞榜樣,以較早使用創建的URL。改爲將它改爲GET。直接使用.html()的主要問題是處理用戶輸入的不可信數據。如果VAR結果返回以下值:而預期值爲
<center>Node 1</center>
嘿謝謝!如果我的結果返回html和一個值,該怎麼辦?如
如果你希望你的值應該被解釋爲html,那麼使用'.html()',並且當你想要將html標籤作爲文本打印出來,就像在這個場景中一樣,然後使用'.text()'作爲我的答案。 CodeBender – 2014-09-26 10:25:23
關注的是,如何使用.html() – CodeBender 2014-09-26 10:29:44