SoFunction
Updated on 2025-03-06

About the issue of cross-domain access of ajax in C#

Recently, due to project needs, cross-domain requests are required to access data. What does cross-domain access mean?

[Cross-domain]: refers to the browser cannot execute scripts from other websites. It is caused by the browser's homologous policy and 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, I have 2 servers on my computer: For example, I have 2 servers on 192.168.0.11 and 192.168.0.12. If the page on the first server wants to access the data on the second server, it is called cross-domain. orTo accessDifferent domain names are also cross-domain. The complete request case is given below:

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/",
    data: { "share_name": encodeURI(name), "telphone": encodeURI(phone), "fromtype": 4 },
    dataType : "jsonp",
    jsonp: "callback",
    jsonpCallback: "successcallback",
    success: function (json) {
     alert();
    },
    error:function(e){
     alert("Submission failed! Please try again later");
    }
   });
  }
 </script>

General handler code fragment:

public class ResourceInsert : IHttpHandler
 {
  public void ProcessRequest(HttpContext context)
  {
    = "application/json";
    = .UTF8;
    model = new ();
    bll = new ();
   //What you need to do   model.share_name = (["share_name"]);
   model.ask_telphone = (["telphone"]);
   model.back_row_one = ["fromtype"];
   ConvertHelper ch = new ConvertHelper();
   model.share_name = (model.share_name);
   //Successcallback is a cross-domain request callback function, remember that it is essential.  The method of obtaining can also be ["callback"].   //The jsonp and jsonpCallback formats corresponding to the front-end page initiated the request are: jsonp_value=jsonpCallback_value   if ((model.share_name, model.ask_telphone))
   {
    Message temp = new Message(1, "We have received your request amount! Please do not submit it repeatedly!", null);
    ("successcallback" + "(" + (temp) + ")");
    ();
    return;
   }
   else
   {
    if ((model) > 0)
    {
     Message temp = new Message(1, "The submission is successful, our staff will reply to you as soon as possible! Thanks for your attention!", null);
     ("successcallback" + "(" + (temp) + ")");
     ();
     return;
    }
    else
    {
     Message temp = new Message(0, "Please confirm that the information is correct!", null);
     ("successcallback" + "(" + (temp) + ")");
     ();
     return;
    }
   }
  }
  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }

Do you think it's over here? Of course not/squinted smile. Of course, it is not possible to miss the configuration file. The following configurations are added under the nodes in the file:

<>
 <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>
 </>

The above is the cross-domain access issue of ajax in C# introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!