Jquery 如何通过ajax从另一个域加载css文件?

Jquery 如何通过ajax从另一个域加载css文件?,jquery,ajax,cross-browser,cross-domain,Jquery,Ajax,Cross Browser,Cross Domain,如何使用jQuery从另一个域通过ajax加载css文件 下载后,请拨打我的回拨电话-以便我可以继续工作。由于限制,您无法实现这一点。与其使用AJAX,为什么不使用标记直接包含这些CSS样式?如果您可以访问可以运行服务器端代码的服务器,您可以执行以下操作: 要求 用于调用位于同一服务器上的scraper的jQuery脚本 用于处理url抓取的服务器端语言(在本例中,使用PHPW/curl) PHP刮刀 你为什么要这么做?为什么不使用一个简单的?但在这种情况下,加载CSS文件后如何调用回调?为

如何使用jQuery从另一个域通过ajax加载css文件


下载后,请拨打我的回拨电话-以便我可以继续工作。

由于限制,您无法实现这一点。与其使用AJAX,为什么不使用
标记直接包含这些CSS样式?

如果您可以访问可以运行服务器端代码的服务器,您可以执行以下操作:

要求
  • 用于调用位于同一服务器上的scraper的jQuery脚本
  • 用于处理url抓取的服务器端语言(在本例中,使用PHPW/curl)
PHP刮刀
你为什么要这么做?为什么不使用一个简单的
?但在这种情况下,加载CSS文件后如何调用回调?为什么要在加载CSS文件后调用回调?如果在JS中,我将开始定义对象的宽度或其他参数,并且没有应用CSS规则(因为文件没有完全加载),然后我得到了错误的对象宽度大小(或其他属性的值)
<?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
    // ...
});