jquery Ajax GET POST cross-domain request implementation

Source: Internet
Author: User

The same logic code needs to be used in multiple sites, each site creates a new ashx is really a rip-off, so want to only request a ashx, so easy to maintain and modify, then, Ajax cross-domain problem comes.

Less nonsense, directly on the code, what I'm doing now is GET request. The same is the POST request.

First, the ashx is rectified, and the code that supports cross-domain requests is added.

Context. Response.ContentType ="Text/plain"; stringActive = context. request.querystring["Active"]; stringrs ="0"; if(Active = ="3")        {            stringOID = TGM. BaseOpera.String.replacesql (context. request.querystring["OID"]); if(!string. IsNullOrEmpty (OID)) {Tansar. Bll.order TBO=NewTansar.                Bll.order (); stringFlag =TBO.                GetSID (OID); if(Flag! =" +") RS="OK"; }        }        #regionSupport cross-domain requestscontext.        Response.ClearHeaders (); stringOrigin = context. request.headers["Origin"]; Context. Response.appendheader ("Access-control-allow-origin",string. IsNullOrEmpty (Origin)?"*": Origin); stringRequestheaders = context. request.headers["access-control-request-headers"]; Context. Response.appendheader ("access-control-allow-headers",string. IsNullOrEmpty (requestheaders)?"*": Requestheaders); Context. Response.appendheader ("Access-control-allow-methods","POST, GET, OPTIONS"); #endregion context. Response.Write (RS);

Then the JS method in Ajax:

functionGetflag (d) {$.ajax ({type:"Get", Async:false, URL:"Http://www.8kmm.com",
Data:D,
DataType:"Text",
Success:function(data) {
if (data = = "OK") {
Location.href = "/user/orderdetail.aspx?oid=<%=onumber%>";}
},
Error: function (XMLHttpRequest, Textstatus, Errorthrown) {
alert ("Request data exception:" + Errorthrown);
}
});

}

To do front-end development, browser developer tools can be a big help, such as the WebKit kernel, ff.

jquery Ajax GET POST cross-domain request implementation

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.