Jquery封装Ajax过滤XSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
$(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);
     }
});*/





本文转自 梦朝思夕 51CTO博客,原文链接:http://blog.51cto.com/qiangmzsx/1385519



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值