Ajax Cross-Domain solutions

Source: Internet
Author: User

From the day of the advent of Ajax, the problem of XMLHttpRequest objects being unable to cross-domain requests has always been a classic problem, due to JavaScript's homologous strategy.

There are several ways to solve this problem:

1. How to use the middle tier transition (can be understood as "proxy")

Intermediate transition, is in the Ajax and different domains of the server communication between a layer of transition, this layer of transition can be PHP, JSP, C + + and any network communication functions of the language, from the middle tier to different domain servers to read data operations. Take an example of ASP. If you need to communicate to an ASP. XMLHttpRequest of a different domain, now the client is going to query one of the domain's ASP. This ASP. NET from this domain is then communicated to the different domains of ASP. NET response Output (response) by this domain.

2. Using the

This method uses src from the

Page code:

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <title>Ajax cross-domain issues</title>    <Scripttype= "Text/javascript"src=""ID= "Getaspx">    </Script>    <Scripttype= "Text/javascript">        functionget (URL) {varobj=document.getElementById ("getaspx"); OBJ.SRC=URL; (Obj.readstatus==  $) {alert (responseval);//If successful, it pops up Dylan            }        }        functionquery () {get (getdemo.aspx); }    </Script></Head><Body><inputtype= "button"value= "Ajax cross-domain test"onclick= "query ();"/></Body></HTML>
View Code

Getdemo.aspx Background code:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;namespace learnjs{public    partial class GetDemo:System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {            Response.Write ("var responseval= ' Dylan '");        }    }}
View Code

This method, also called Ajaj or Ajax without XMLHttpRequest, converts x to J because it uses

3. jquery Solution

Page code:

<HTML><Head><title>jquery Learning</title><Scriptsrc= "Jquery-1.4.2.min.js"type= "Text/javascript"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){    varobtntest= $("#btnTest"); Obtntest.click (function() {obtntest.disabled= true; varOresult= $("#result"); Oresult.html ("Loading"). CSS ("Color","Red"); Jquery.getscript ("Http://www.test.com/test/js.txt",         function() {oresult.html ("Name:" +Dylan.name+ "<br/>email:" +dylan.email). CSS ("Color","Black"); Obtntest.disabled= false;             });    });    }); </Script></Head><Body><ButtonID= "Btntest">Btntest</Button><DivID= "Result"></Div></Body></HTML>
View Code

The contents of the remote server-side Js.txt are:

var dylan= {name: "Dylan", Email:[email protected]}

Ajax Cross-Domain solutions

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.