简体   繁体   English

jQuery Ajax防止XSS

[英]Jquery ajax prevent xss

I receive information from server (user_agent string) by getJSON and insert it into the table. 我通过getJSON从服务器(user_agent字符串)接收信息,并将其插入表中。 But I think that the code below is not safe because somebody can change user_agent variable to inject a string with related consequences. 但是我认为下面的代码并不安全,因为有人可以更改user_agent变量以注入具有相关后果的字符串。

In code below the value is the server's returned string. 在下面的代码中,该value是服务器的返回字符串。

$.each(this, function(){
    var new_row="";
    var columns="";
    $.each(this, function(key,value) {
        columns+="<td>"+value+"</td>";
    });
    new_row+='<div id="whoer_rows" style="display:none"><table border="1"><tr>'+columns+'</tr></table></div>';
});

I tried to use jquery's method text() like: 我试图使用jquery的方法text()像:

columns+=$("<td></td>").text(value);

But I can't to adapt new_row to correct jquery syntax 但是我无法适应new_row以更正jquery语法

Solve it! 解决这个问题!

$.each(this, function(){
    var columns="";
    $.each(this, function(key,value) {
        columns+="<td>"+value+"</td>";
    });

    var $tr=$("<tr></tr>").append(columns);
    var $table=$("<table></table>");
    var $div=$("<div></div>");

    $table.append($tr).appendTo($div);
    $div.prependTo("#whoer");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM