开发者

How load css file via ajax from another domain?

开发者 https://www.devze.com 2023-01-27 02:39 出处:网络
How use jQuery load css file via ajaxfrom another domain? And after downloa开发者_如何学Pythonding call my callback - so I could continue working.If you have access to a server that can run server-si

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

And after downloa开发者_如何学Pythonding call my callback - so I could continue working.


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消