On cross domain access of ajax in C

  • 2021-12-13 16:42:27
  • OfStack

Recently, due to the needs of the project, it is necessary to request access to data across domains. What does cross-domain access mean?

Cross-domain: Means that browsers cannot execute scripts from other Web sites. It is caused by the browser's homologous policy, which is the security restriction imposed by the browser on JavaScript. The so-called same domain means that the domain name, protocol and port are all the same. It doesn't matter if you don't understand it. For example, there are two servers 192.168. 0.11 and 192.168. 0.12 on my computer. If a page on Server 1 wants to access data on Server 2, it is called cross-domain. Or http://www.baidu.com to access http://www.xxx.com is also a different domain name and cross-domain. The following is a complete request case:

Front-end page request code slice:


<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(" Submission failed! Please try again later ");
    }
   });
  }
 </script>

1 generic handler code slice:


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();
   // What you need to do 
   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 Request callback functions for cross-domains, remember that they are essential. You can also get it by context.Request["callback"] , 
   // Object that initiates the request corresponding to the front-end page jsonp And jsonpCallback Format is :jsonp_value=jsonpCallback_value
   if (bll.Exists(model.share_name, model.ask_telphone))
   {
    Message temp = new Message(1, " We have received your request amount! Please do not submit it repeatedly! ", null);
    context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
    context.Response.End();
    return;
   }
   else
   {
    if (bll.Add(model) > 0)
    {
     Message temp = new Message(1, " If the submission is successful, our staff will reply to you as soon as possible! Thank you for your attention! ", null);
     context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
     context.Response.End();
     return;
    }
    else
    {
     Message temp = new Message(0, " Please make sure the information is filled in correctly! ", 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/squint. Of course, it can't be less in the configuration file. Add the following configuration under system. webServer node in 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>

Related articles: