Example of ajax cross-origin data reading using jquery

Source: Internet
Author: User
Tags json php code

We know that using server scripts for js can implement communication. However, the function is too simple and not flexible enough, so it still requires convenient communication like ajax. Today, the company encountered an ajax cross-origin problem when using phonegap. I eavesdropped and summarized the two cross-origin problems of ajax. Hey. Of course, there is a premise for such application scenarios. Generally, we use the same domain name, but there is no domain for client software or mobile phone applications, so cross-domain communication needs to be resolved.

If JavaScript is to be cross-origin, you can use:

<Script src = "js addresses of other websites (the inner space can be dynamically generated, such as aaa. jsp and bbb. aspx. "> </Script> You can read data from other websites.
The key is to see if your spirit is not flexible and you will use it.
 
To cross-origin ajax, you can use the server to retrieve content from other websites, such as asp.net:

 

The code is as follows: Copy code
Public string GetUrlData (string url)
{
System. Net. HttpWebRequest webRequest = (System. Net. HttpWebRequest) System. Net. WebRequest. Create (url );
System. Net. WebResponse webResponse = webRequest. GetResponse ();
System. IO. Stream iStream = webResponse. GetResponseStream ();
System. IO. StreamReader sr = new System. IO. StreamReader (iStream, System. Text. Encoding. Default );
String str = sr. ReadToEnd ();
Sr. Close ();
IStream. Close ();
WebResponse. Close ();
Return str;
}

This method has been written. You only need to input the url of the website to obtain the ajax return value.
In this way, the ajax requests of other websites are written to your own server, and then you can use ajax to request the one you wrote.
The aspx address is enough.
 
The same applies to java, for example:

The code is as follows: Copy code

URL url = new URL ("http://www.111cn.net /");
FilterInputStream f = (FilterInputStream) url. openStream ();

Another method is to use jsonp

You can check this and paste the example here.
Html code

The code is as follows: Copy code

<! Doctype html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Meta name = "Generator" content = "EditPlus®">
<Meta name = "Author" content = "">
<Meta name = "Keywords" content = "">
<Meta name = "Description" content = "">
<Title> Document </title>
</Head>
<Body>
<Script src = "jquery-1.8.3.min.js"> </script>
<Script type = "text/javascript">
JQuery (document). ready (function (){
$. Ajax ({
Type: "GET ",
Async: false,
// Url: "http: // test/jsonp. php ",
Url: "http://local. OK .com/1.php ",
DataType: "jsonp ",
Jsonp: "callback", // The parameter name that is passed to the request handler or page to obtain the jsonp callback function name (generally: callback by default)
JsonpCallback: "bbb", // custom jsonp callback function name. The default value is the random function name automatically generated by jQuery. You can also enter "? ", JQuery will automatically process the data for you
Success: function (json ){
Alert ('You found the flight information: fare: '+ json. price +' yuan, remaining ticket: '+ json. tickets +. Callback function name: '+ json. func );
},
Error: function (){
Alert ("fail ");
             } 
});
});
</Script>

</Body>
</Html>

A/1. php code

The code is as follows: Copy code

<? Php
$ Callback = isset ($ _ GET ["callback"])? $ _ GET ["callback"]: 'flighthandler ';
$ A = array (
'Code' => '000098 ',
'Price' => '123 ',
'Tickets '=> 20,
'Function' => $ callback,
);
$ Result = json_encode ($ );
Echo "{$ callback} ($ result )";
Exit;
?>

You can directly double-click to run html or run it under another domain name host to implement communication.

The second method is to add header information to server scripts such as php.

The code is as follows: Copy code


Header ('Access-Control-Allow-Origin :*');

In this way, the return value of the script can be used in any domain, and ajax can be used in the original usage.

The latter part is from the previous part of the blog 521php.com.

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.