


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
