如何通过来自其他域的ajax使用jQuery加载css文件?
下载后拨打我的回叫 - 所以我可以继续工作。
答案 0 :(得分:1)
如果您可以访问可以运行服务器端代码的服务器,那么您可以采取以下措施:
<?php
/**
* Receives a url and optional callback, scrapes the url, and returns the results
* @author Jason Featheringham
* @link http://thejase.com
*/
/**
* Retrieves a web page, including content, error and header information
* @param string $url A web address to fetch
* @return array The results of the screen scrape attempt
*/
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$result = curl_getinfo( $ch );
$result['content'] = curl_exec( $ch );
$result['error']['number'] = curl_errno( $ch );
$result['error']['message'] = curl_error( $ch );
curl_close( $ch );
return $result;
}
// either fetch web page or generate error message for result
$result = json_encode( ( $url = $_GET['url'] )
? get_web_page( $url )
: Array( "error" => Array( "message" => "You must specify a url parameter." ) ) );
// if callback is specified, return JSONP result, otherwise just JSON
echo ( $callback = $_GET['callback'] )
? header("text/javascript") ?: "$callback($result);"
: header("application/json") ?: $result;
?>
$.getJSON( "scraper.php?url=http://www.yahoo.com&callback=?", function(result) {
if( result.error ) {
// handle error
}
// otherwise, use the result object (usually result.content) as you see fit
// ...
});
答案 1 :(得分:0)
由于same origin policy限制,您无法实现此目的。而不是使用AJAX,为什么不直接使用<link>
标签包含那些CSS样式?