Home >Backend Development >C#.Net Tutorial >Detailed introduction to ajax cross-domain access issues in C#
Recently working on a project that requires cross-domain requests to access data. The following is a detailed explanation of the ajax cross-domain access code in C#. Friends who need it can refer to
. Recently, due to project needs, cross-domain requests are required to access data. What does cross-domain access mean?
[Cross-domain]: Refers to the fact that the browser cannot execute scripts from other websites. It is caused by the browser's same-origin policy, which is a security restriction imposed by the browser on JavaScript. The so-called same domain means that the domain name, protocol, and port are the same. It doesn’t matter if you don’t understand. For example, there are two servers on my computer, 192.168.0.11 and 192.168.0.12. If a page on the first server needs to access data on the second server, it is called cross-domain. Or http://www.baidu.com to access www.xxx.com is also a different domain name and cross-domain. The complete request case is given below:
Front-end page request code piece:
<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>
General handler code piece:
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为跨域请求回调函数,切记必不可少。获取方式也可以为context.Request["callback"], //对应前端页面发起请求的jsonp和jsonpCallback格式为:jsonp_value=jsonpCallback_value if (bll.Exists(model.share_name, model.ask_telphone)) { Message temp = new Message(1, "我们已收到您的请求额!请勿重复提交!", null); context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")"); context.Response.End(); return; } else { if (bll.Add(model) > 0) { Message temp = new Message(1, "提交成功,我们工作人员会尽快回复你!感谢关注!", null); context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")"); context.Response.End(); return; } else { Message temp = new Message(0, "请确认信息填写无误!", null); context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")"); context.Response.End(); return; } } } public bool IsReusable { get { return false; } } }
Do you think it’s over here? Of course not/squinting. Configuration file is of course indispensable. Add the following configuration under the system.webServer node in the web.config file:
<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>
The above is the detailed content of Detailed introduction to ajax cross-domain access issues in C#. For more information, please follow other related articles on the PHP Chinese website!