Loading cross-domain endpoint with AJAX

前端 未结 9 1665
醉酒成梦
醉酒成梦 2020-11-21 05:20

I\'m trying to load a cross-domain HTML page using AJAX but unless the dataType is \"jsonp\" I can\'t get a response. However using jsonp the browser is expecting a script m

9条回答
  •  北海茫月
    2020-11-21 06:00

    jQuery Ajax Notes

    • Due to browser security restrictions, most Ajax requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol.
    • Script and JSONP requests are not subject to the same origin policy restrictions.

    There are some ways to overcome the cross-domain barrier:

    • CORS Proxy Alternatives
    • Ways to circumvent the same-origin policy
    • Breaking The Cross Domain Barrier

    There are some plugins that help with cross-domain requests:

    • Cross Domain AJAX Request with YQL and jQuery
    • Cross-domain requests with jQuery.ajax

    Heads up!

    The best way to overcome this problem, is by creating your own proxy in the back-end, so that your proxy will point to the services in other domains, because in the back-end not exists the same origin policy restriction. But if you can't do that in back-end, then pay attention to the following tips.


    Warning!

    Using third-party proxies is not a secure practice, because they can keep track of your data, so it can be used with public information, but never with private data.


    The code examples shown below use jQuery.get() and jQuery.getJSON(), both are shorthand methods of jQuery.ajax()


    CORS Anywhere

    CORS Anywhere is a node.js proxy which adds CORS headers to the proxied request.
    To use the API, just prefix the URL with the API URL. (Supports https: see github repository)

    If you want to automatically enable cross-domain requests when needed, use the following snippet:

    $.ajaxPrefilter( function (options) {
      if (options.crossDomain && jQuery.support.cors) {
        var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
        options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
        //options.url = "http://cors.corsproxy.io/url=" + options.url;
      }
    });
    
    $.get(
        'http://en.wikipedia.org/wiki/Cross-origin_resource_sharing',
        function (response) {
            console.log("> ", response);
            $("#viewer").html(response);
    });
    


    Whatever Origin

    Whatever Origin is a cross domain jsonp access. This is an open source alternative to anyorigin.com.

    To fetch the data from google.com, you can use this snippet:

    // It is good specify the charset you expect.
    // You can use the charset you want instead of utf-8.
    // See details for scriptCharset and contentType options: 
    // http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
    $.ajaxSetup({
        scriptCharset: "utf-8", //or "ISO-8859-1"
        contentType: "application/json; charset=utf-8"
    });
    
    $.getJSON('http://whateverorigin.org/get?url=' + 
        encodeURIComponent('http://google.com') + '&callback=?',
        function (data) {
            console.log("> ", data);
    
            //If the expected response is text/plain
            $("#viewer").html(data.contents);
    
            //If the expected response is JSON
            //var response = $.parseJSON(data.contents);
    });
    


    CORS Proxy

    CORS Proxy is a simple node.js proxy to enable CORS request for any website. It allows javascript code on your site to access resources on other domains that would normally be blocked due to the same-origin policy.

    • CORS-Proxy gr2m
    • CORS-Proxy rmadhuram

    How does it work? CORS Proxy takes advantage of Cross-Origin Resource Sharing, which is a feature that was added along with HTML 5. Servers can specify that they want browsers to allow other websites to request resources they host. CORS Proxy is simply an HTTP Proxy that adds a header to responses saying "anyone can request this".

    This is another way to achieve the goal (see www.corsproxy.com). All you have to do is strip http:// and www. from the URL being proxied, and prepend the URL with www.corsproxy.com/

    $.get(
        'http://www.corsproxy.com/' +
        'en.wikipedia.org/wiki/Cross-origin_resource_sharing',
        function (response) {
            console.log("> ", response);
            $("#viewer").html(response);
    });
    


    CORS proxy browser

    Recently I found this one, it involves various security oriented Cross Origin Remote Sharing utilities. But it is a black-box with Flash as backend.

    You can see it in action here: CORS proxy browser
    Get the source code on GitHub: koto/cors-proxy-browser

提交回复
热议问题