Csshttprequest: Cross Domain Ajax request for easy

Source: Internet
Author: User
ArticleDirectory
    • Examples examples.

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

 

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.