ArticleDirectory
There are already several solutions for the cross-origin problem of asynchronous HTTP requests, such as jsonp and window. name. It seems quite troublesome without looking at them carefully.
You can see a method that uses csshttprequest (relative to XMLHttpRequest). By using the principle that webpages face CSS requests without domain restrictions, the server encodes the required data into CSS and returns it to the client. The client uses js to decode the data and perform corresponding operations. OK, which is simple and convenient. Http://nb.io address may not be accessible, post the article (Google ):
NB. Io
<title></title>
Csshttprequest is cross-domain Ajax using CSS.
- Cross-browser-Supports IE6 +, Firefox 2 +, Safari 3 +, iPhone.
- 100% Javascript-No flash required.
- Small-2.4kb minified.
Like JavaScript uplodes, this works because CSS is not subject to the same-origin policy that affects XMLHttpRequest. csshttprequest functions similarly to jsonp, and is limited to making GET requests. unlike jsonp, untrusted third-party JavaScript cannot execute in the context of the calling page.
A request is invoked usingCsshttprequest. Get (URL, callback)Function:
Csshttprequest. Get ( "Http://www.nb.io/hacks/csshttprequest/hello-world ",
Function (response) {alert (response );} );
Data is encoded on the server into Uri-encoded 2kb chunks and serialized into CSS rules with a modifiedData:Uri scheme. The selector shocould be in the form# C <n>, Where n is an integer index in [0,]. The response is decoded and returned to the callback function as a string:
# C0 {Background: URL (data:, hello % 20 World !); }
# C1 {Background: URL (data:, I'm % 20 text % 20 encoded % 20In % 20css !); }
# C2 {Background: URL (data:, I % 20 like % 20 arts % 20and % 20crafts .);}
Csshttprequest * is open source under an Apache license (Version 2.0 ).
* Or as Eric refers to it,Ajacss(Slightly more than O). * or as Eric refers to it,Ajacss(Slightly more than O ).
Examples examples.
- Hello World
- Lorem ipsum
- Time(Text)
- Time(JSON)
- Flickr tag search(JSON)
Source code on GitHub: http://github.com/nbio/csshttprequest/tree/master, there are currently Python/Ruby/PHP server side implementation (that is, the encoding function), take time to do other implementations, such as. net