最近因項目需要,需要跨域請求訪問數(shù)據(jù)。跨域訪問是指什么?
[跨域]:指的是瀏覽器不能執(zhí)行其他網(wǎng)站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript施加的安全限制。所謂同域是指,域名,協(xié)議,端口均相同,不明白沒關(guān)系,舉個栗子:例如,我的電腦上有2個服務(wù)器 192.168.0.11和192.168.0.12。如果第一個服務(wù)器上的頁面要訪問第二個服務(wù)器上面的數(shù)據(jù),就叫做跨域。或者http://www.baidu.com 要訪問http://www.xxx.com也是不同域名也是跨域。 下面給出完整請求案例:
前端頁面請求代碼片:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<script type= "text/javascript" > function ajaxsubmit(name,phone) { $.ajax({ type: "get" , url: "http://10.10.10.132:35709/AppInterface/ResourceInsert.ashx" , data: { "share_name" : encodeURI(name), "telphone" : encodeURI(phone), "fromtype" : 4 }, dataType : "jsonp" , jsonp: "callback" , jsonpCallback: "successcallback" , success: function (json) { alert(json.msg); }, error:function(e){ alert( "提交失敗!請稍后再試" ); } }); } </script> |
一般處理程序代碼片:
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
|
public class ResourceInsert : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json" ; context.Response.ContentEncoding = System.Text.Encoding.UTF8; cms.Model.Resource model = new Model.Resource(); cms.BLL.Resource bll = new BLL.Resource(); //你所需要進行的操作 model.share_name = HttpUtility.UrlDecode(context.Request[ "share_name" ]); model.ask_telphone = HttpUtility.UrlDecode(context.Request[ "telphone" ]); model.back_row_one = context.Request[ "fromtype" ]; ConvertHelper ch = new ConvertHelper(); model.share_name = ch.RemoveSpecialChar(model.share_name); //successcallback為跨域請求回調(diào)函數(shù),切記必不可少。獲取方式也可以為context.Request["callback"], //對應(yīng)前端頁面發(fā)起請求的jsonp和jsonpCallback格式為:jsonp_value=jsonpCallback_value if (bll.Exists(model.share_name, model.ask_telphone)) { Message temp = new Message(1, "我們已收到您的請求額!請勿重復(fù)提交!" , null ); context.Response.Write( "successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")" ); context.Response.End(); return ; } else { if (bll.Add(model) > 0) { Message temp = new Message(1, "提交成功,我們工作人員會盡快回復(fù)你!感謝關(guān)注!" , null ); context.Response.Write( "successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")" ); context.Response.End(); return ; } else { Message temp = new Message(0, "請確認(rèn)信息填寫無誤!" , null ); context.Response.Write( "successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")" ); context.Response.End(); return ; } } } public bool IsReusable { get { return false ; } } } |
你以為到這里完了嗎?當(dāng)然沒有/斜眼笑。配置文件中當(dāng)然不能少,web.config文件中的 system.webServer 節(jié)點下 增加如下配置:
1
2
3
4
5
6
7
8
9
|
<system.webServer> <httpProtocol> <customHeaders> <add name= "Access-Control-Allow-Methods" value= "OPTIONS,POST,GET" /> <add name= "Access-Control-Allow-Headers" value= "x-requested-with,content-type" /> <add name= "Access-Control-Allow-Origin" value= "*" /> </customHeaders> </httpProtocol> </system.webServer> |
以上所述是小編給大家介紹的關(guān)于C#中ajax跨域訪問問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/cdl_darren/article/details/71730052