Loading cross-domain endpoint with AJAX

前端 未结 9 1662
醉酒成梦
醉酒成梦 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 05:39

    To get the data form external site by passing using a local proxy as suggested by jherax you can create a php page that fetches the content for you from respective external url and than send a get request to that php page.

    var req = new XMLHttpRequest();
    req.open('GET', 'http://localhost/get_url_content.php',false);
    if(req.status == 200) {
       alert(req.responseText);
    }
    

    as a php proxy you can use https://github.com/cowboy/php-simple-proxy

    0 讨论(0)
  • 2020-11-21 05:43

    If the external site doesn't support JSONP or CORS, your only option is to use a proxy.

    Build a script on your server that requests that content, then use jQuery ajax to hit the script on your server.

    0 讨论(0)
  • 2020-11-21 05:44

    Just put this in the header of your PHP Page and it ill work without API:

    header('Access-Control-Allow-Origin: *'); //allow everybody  
    

    or

    header('Access-Control-Allow-Origin: http://codesheet.org'); //allow just one domain 
    

    or

    $http_origin = $_SERVER['HTTP_ORIGIN'];  //allow multiple domains
    
    $allowed_domains = array(
      'http://codesheet.org',
      'http://stackoverflow.com'
    );
    
    if (in_array($http_origin, $allowed_domains))
    {  
        header("Access-Control-Allow-Origin: $http_origin");
    }
    
    0 讨论(0)
  • 2020-11-21 05:45

    You need CORS proxy which proxies your request from your browser to requested service with appropriate CORS headers. List of such services are in code snippet below. You can also run provided code snippet to see ping to such services from your location.

    $('li').each(function() {
      var self = this;
      ping($(this).text()).then(function(delta) {
        console.log($(self).text(), delta, ' ms');
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.rawgit.com/jdfreder/pingjs/c2190a3649759f2bd8569a72ae2b597b2546c871/ping.js"></script>
    <ul>
      <li>https://crossorigin.me/</li>
      <li>https://cors-anywhere.herokuapp.com/</li>
      <li>http://cors.io/</li>
      <li>https://cors.5apps.com/?uri=</li>
      <li>http://whateverorigin.org/get?url=</li>
      <li>https://anyorigin.com/get?url=</li>
      <li>http://corsproxy.nodester.com/?src=</li>
      <li>https://jsonp.afeld.me/?url=</li>
      <li>http://benalman.com/code/projects/php-simple-proxy/ba-simple-proxy.php?url=</li>
    </ul>

    0 讨论(0)
  • 2020-11-21 05:48

    Figured it out. Used this instead.

    $('.div_class').load('http://en.wikipedia.org/wiki/Cross-origin_resource_sharing #toctitle');
    
    0 讨论(0)
  • 2020-11-21 05:54

    Your URL doesn't work these days, but your code can be updated with this working solution:

    var url = "http://saskatchewan.univ-ubs.fr:8080/SASStoredProcess/do?_username=DARTIES3-2012&_password=P@ssw0rd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute";
    
    url = 'https://google.com'; // TEST URL
    
    $.get("https://images"+~~(Math.random()*33)+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=" + encodeURI(url), function(data) {
        $('div.ajax-field').html(data);
    });
    <div class="ajax-field"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    0 讨论(0)
提交回复
热议问题