Perfect solution to Ajax Cross-domain problem _javascript Skills

Source: Internet
Author: User

Since the day Ajax was born, the problem of XMLHttpRequest objects that cannot be requested across domains persists. This seems to be a very classic question. is due to JavaScript's homology strategy, which is not discussed in depth here.

There are probably several solutions to this problem:

1. How to use the middle-tier transition (understandable as "agent"):

Intermediate transition, it is obvious that in the Ajax and different domains of the server communication between the middle of the transition, this layer of transition can be PHP, JSP, C + +, and any other network communication functions of the language, by the middle tier to different domains of the server to read data operations. Take asp.net to do an example, if you need to communicate with a asp.net in a different domain, now the client's XMLHttpRequest first query a asp.net in this domain, The asp.net of the domain then communicates with the asp.net of the different domains, and then the asp.net response output (response) of the domain; the idea is probably this, and I believe the reader has already understood it very clearly.

2. Use the

This method uses SRC in the

For example, let's take a closer look:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>ajax cross-domain issues </title>
<script type= "Text/javascript" src= "id=" getaspx ">
</script>
<script type= "Text/javascript" >
function get (URL) {
var obj = document.getElementById ("getaspx");
obj.src = URL;
(Obj.readstatus = 200)
{
alert (responseval);//If successful, pops up Dylan
}
}
function query () {
Get (getdemo.aspx);
}
</script>
<body>
<input type= "button" value= "Ajax cross-domain Test" onclick= "query ();" />
</body>

Getdemo.aspx Background code:
Copy Code code as follows:

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 '");
}
}
}


This method, also known as Ajaj or Ajax without XMLHttpRequest, replaces X with J, because the <script> tag is used instead of XML and XMLHttpRequest. This method seems a little "alternative", haha.

Now let's see how we can solve the cross-domain problem of Ajax after having jquery:

Copy Code code as follows:

<title>jquery Learning </title>
<script src= "Jquery-1.4.2.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
var obtntest = $ ("#btnTest");
Obtntest.click (function () {
Obtntest.disabled = true;
var Oresult = $ ("#result");
Oresult.html ("Loading"). CSS ("Color", "red");
Jquery.getscript ("Http://www.jb51.net/test/js.txt",
function () {
Oresult.html ("Name:" + Dylan.name + "<br/>email:" + dylan.email). CSS ("Color", "black");
obtntest.disabled = false;
});
});
});
</script>
<body>
<button id= "Btntest" >BtnTest</button>
<div id= "Result" ></div>
</body>

The contents of the remote server-side Js.txt are:
var dylan= {name: "Dylan", email:dylan@163.com}

I feel this way more concise. Oh. Of course, the reader can choose the implementation way according to the actual situation.

How, in fact, it is very simple, I have seen many people do not want to face up to Ajax technology bottlenecks exist, in fact, Ajax should be Ajax rather than Ajax, highlighting the first A is to emphasize the fact that Ajax is a way to develop asynchronous transmission, rather than exactly what kind of technology used.

In fact, after the JSON data format, there is a more bull X "Jsonp", which enables cross-domain communication of Ajax. In fact, JSONP is not a data format, but the second way I introduced the improvement. Starting with the jQuery1.2 version, JQuery has local support for the JSONP callback.

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.