A detailed introduction to Ajax cross-domain access issues in C #

Source: Internet
Author: User
Recent projects require cross-domain requests for access to data issues. Below through this article to share in C # Ajax cross-domain access code in detail, the need for friends can refer to the following

Recently, because of project needs, cross-domain request access to data is required. What does cross-domain access mean?

[Cross-domain]: refers to the browser is unable to execute other Web site scripts. It is caused by the browser's homologous policy, which is the security restrictions that the browser imposes on JavaScript. The so-called same domain refers to, domain names, protocols, ports are the same, do not understand it's OK, to raise a chestnut: for example, My computer has 2 servers 192.168.0.11 and 192.168.0.12. If the page on the first server accesses the data above the second server, it is called a cross-domain. or http://www.baidu.com to access www.xxx.com is also different domain names are also cross-domain. The complete request case is given below:

Front page Request code slices:

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

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 for cross-domain request callback functions, remember that it is necessary. The access method can also be a context. Request["Callback"],//JSONP and Jsonpcallback for front-end page initiation requests are: Jsonp_value=jsonpcallback_value if (BLL. Exists (Model.share_name, Model.ask_telphone)) {Message temp = new Message (1, "We have received your request amount! Do not repeat submissions!    ", null); Context.    Response.Write ("Successcallback" + "(" + jsonconvert.serializeobject (temp) + ")"); Context. REsponse.    End ();   Return } else {if (BLL. ADD (model) > 0) {Message temp = new Message (1, "submitted successfully, our staff will reply you as soon as possible!") Thanks for your attention!     ", null); Context.     Response.Write ("Successcallback" + "(" + jsonconvert.serializeobject (temp) + ")"); Context.     Response.End ();    Return } else {Message temp = new Message (0, "Please confirm that 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 you're done here? Of course not/squint laugh. The configuration file is certainly not less, and the following configuration is added under the System.webserver node in the Web. config file:

<system.webServer> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.