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

发布于 2024-10-04 00:31:09 字数 74 浏览 4 评论 0原文

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

下载后致电我的回调 - 这样我就可以继续工作。

How use jQuery load css file via ajax from another domain?

And after downloading call my callback - so I could continue working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-10-11 00:31:09

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

要求

  • jQuery 脚本来调用位于同一服务器上的抓取器
  • 服务器端语言来处理 url 抓取(在本例中,PHP w /curl 被使用)

PHP Scraper

<?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;
?>

jQuery 代码

$.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
    // ...
});

If you have access to a server that can run server-side code, you could do something along the lines of this:

Requirements

  • jQuery script to call scraper located on same server
  • Server-side language to handle url scraping (in this case, PHP w/ curl is used)

PHP Scraper

<?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;
?>

jQuery Code

$.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
    // ...
});
一刻暧昧 2024-10-11 00:31:09

由于同源政策限制,您无法实现此目的。为什么不使用 AJAX 直接使用 标签包含这些 CSS 样式?

You cannot achieve this due to same origin policy restrictions. Instead of using AJAX why not directly include those CSS styles using the <link> tag?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文