Detailed introduction to ajax cross-domain access issues in C#

黄舟
Release: 2017-05-14 10:49:30
Original
2511 people have browsed it

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:

Copy after login

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;
   }
  }
 }
Copy after login

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:


 
  
  
  
  
  
 
 
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!