Jquery封装Ajax过滤XSS

$(document).ajaxSend(onSend);
function onSend(e,xhr,o) {
    o.data=dataEncode(o.data);
};
function htmlEncode (str){
    var s = "";
    if (str.length == 0) return "";
    //s = str.replace(/ /g, " ");
    //s = str.replace(/&/g, "&");
    s = str.replace(/</g, "%26lt%3B");
    s=s.replace(/%3C/g,"%26lt%3B");
    s=s.replace(/%3c/g,"%26lt%3B");
    s = s.replace(/>/g, "%26gt%3B");
    s = s.replace(/%3E/g, "%26gt%3B");
    s = s.replace(/%3e/g, "%26gt%3B");
    //s = s.replace(/\'/g, "&#39;");
    //s = s.replace(/\"/g, "&quot;");
    //s = s.replace(/\n/g, "<br>");
    return s;
};
function htmlEncodeOut (str){
    var s = "";
    if (str.length == 0) return "";
    //s = str.replace(/ /g, "&nbsp;");
    //s = str.replace(/&/g, "&amp;");
    s = str.replace(/</g, "&lt;");
    s=s.replace(/%3C/g,"&lt;");
    s=s.replace(/%3c/g,"&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/%3E/g, "&gt;");
    s = s.replace(/%3e/g, "&gt;");
    s = s.replace(/%26lt%3B/g, "&lt;");
    s = s.replace(/%26lt%3b/g, "&lt;");
    s = s.replace(/%26gt%3B/g, "&gt;");
    s = s.replace(/%26gt%3b/g, "&gt;");
    //s = s.replace(/\'/g, "&#39;");
    //s = s.replace(/\"/g, "&quot;");
    //s = s.replace(/\n/g, "<br>");
    return s;
};
function dataEncode(data){
    var rel=data;
    var source="";
    if(typeof(rel) == "object"){
        source=htmlEncode(JSON.stringify(rel));
        source=JSON.parse(source);
        rel=source;
    }else if(typeof(rel) == "string"){
        source=htmlEncode(rel);
        rel=source;
    }
    return rel;
};
function dataEncodeOut(data){
    var rel=data;
    var source="";
    if(typeof(rel) == "object"){
        source=htmlEncodeOut(JSON.stringify(rel));
        source=JSON.parse(source);
        rel=source;
    }else if(typeof(rel) == "string"){
        source=htmlEncodeOut(rel);
        rel=source;
    }
    return rel;
};
/*
//此处的Ajax请求是一个示例;未使用框架封装的Ajax请求时,这需要在自己的ajax方法的success函数的首行添加-- data=dataEncodeOut(data);
jQuery.ajax({ 
    type:"post",
    data:data,
    url:"http://dfh.smartcity.com/portal/app/service/App.getStateCatalogues.json", 
    async:async,
    dataType:"json", 
    success: function aa(data) {
        //需要首行添加
        data=dataEncodeOut(data);
        alert("success已经收到:"+data); 
    }
});*/

本文题目:Jquery封装Ajax过滤XSS
转载源于:https://www.cdcxhl.com/article2/jhspic.html

成都网站建设公司_创新互联,为您提供微信小程序商城网站软件开发网站排名Google

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站