Loading cross-domain endpoint with AJAX

前端 未结 9 1673
醉酒成梦
醉酒成梦 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: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");
    }
    

提交回复
热议问题