Origin http://localhost is not allowed by Access-Control-Allow-Origin.?

前端 未结 3 1609
渐次进展
渐次进展 2020-12-04 18:31

i have a problem... i try to get json api in \"http://api.master18.tiket.com/search/autocomplete/hotel?q=mah&token=90d2fad44172390b11527557e6250e50&secretkey

相关标签:
3条回答
  • 2020-12-04 18:59

    You've got two ways to go forward:

    JSONP


    If this API supports JSONP, the easiest way to fix this issue is to add &callback to the end of the URL. You can also try &callback=. If that doesn't work, it means the API does not support JSONP, so you must try the other solution.

    Proxy Script


    You can create a proxy script on the same domain as your website in order to avoid the cross-origin issues. This will only work with HTTP URLs, not HTTPS URLs, but it shouldn't be too difficult to modify if you need that.

    <?php
    // File Name: proxy.php
    if (!isset($_GET['url'])) {
        die(); // Don't do anything if we don't have a URL to work with
    }
    $url = urldecode($_GET['url']);
    $url = 'http://' . str_replace('http://', '', $url); // Avoid accessing the file system
    echo file_get_contents($url); // You should probably use cURL. The concept is the same though
    

    Then you just call this script with jQuery. Be sure to urlencode the URL.

    $.ajax({
        url      : 'proxy.php?url=http%3A%2F%2Fapi.master18.tiket.com%2Fsearch%2Fautocomplete%2Fhotel%3Fq%3Dmah%26token%3D90d2fad44172390b11527557e6250e50%26secretkey%3D83e2f0484edbd2ad6fc9888c1e30ea44%26output%3Djson',
        type     : 'GET',
        dataType : 'json'
    }).done(function(data) {
        console.log(data.results.result[1].category); // Do whatever you want here
    });
    

    The Why


    You're getting this error because of XMLHttpRequest same origin policy, which basically boils down to a restriction of ajax requests to URLs with a different port, domain or protocol. This restriction is in place to prevent cross-site scripting (XSS) attacks.

    More Information

    Our solutions by pass these problems in different ways.

    JSONP uses the ability to point script tags at JSON (wrapped in a javascript function) in order to receive the JSON. The JSONP page is interpreted as javascript, and executed. The JSON is passed to your specified function.

    The proxy script works by tricking the browser, as you're actually requesting a page on the same origin as your page. The actual cross-origin requests happen server-side.

    0 讨论(0)
  • 2020-12-04 19:07

    I fixed this (for development) with a simple nginx proxy...

    # /etc/nginx/sites-enabled/default
    server {
      listen 80;
      root /path/to/Development/dir;
      index index.html;
    
      # from your example
      location /search {
        proxy_pass http://api.master18.tiket.com;
      }
    }
    
    0 讨论(0)
  • 2020-12-04 19:11

    a thorough reading of jQuery AJAX cross domain seems to indicate that the server you are querying is returning a header string that prohibits cross-domain json requests. Check the headers of the response you are receiving to see if the Access-Control-Allow-Origin header is set, and whether its value restricts cross-domain requests to the local host.

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