获取jQuery UI主题列表 - 来自URL(同源策略)

时间:2012-09-26 22:33:33

标签: javascript jquery cross-domain same-origin-policy

有谁知道从http://jquery-ui.googlecode.com/svn/tags/1.8.23/themes/获取jQuery主题列表的方法?

我正在使用主题滚动创建简单的网页,用户可以动态切换主题。

Working fiddle - 点击右上角的主题,然后选择一个新主题。

现在列表硬编码如下,

<div id="theme-list">    
   <ul>
      <li class="themes-el ui-state-highlight" data-theme="cupertino">cupertino</li>
      <li class="themes-el" data-theme="hot-sneaks">hot-sneaks</li>
      <li class="themes-el" data-theme="smoothness">smoothness</li>
      <li class="themes-el" data-theme="pepper-grinder">pepper-grinder</li>
      <li class="themes-el" data-theme="ui-lightness">ui-lightness</li>
      <li class="themes-el" data-theme="ui-darkness">ui-darkness</li>
      <!-- and more -->
   </ul>    
</div>

有没有办法从网址http://jquery-ui.googlecode.com/svn/tags/1.8.23/themes/获取此主题列表? (crossDomain:http://www.w3.org/TR/cors/#access-control-allow-origin-response-hea

尝试过,但在下面的代码中失败了。

$.ajax({
    url: 'http://jquery-ui.googlecode.com/svn/tags/1.8.23/themes/',
    dataType: 'text',
    beforeSend: function ( xhr ) {
        xhr.setRequestHeader("Access-Control-Allow-Origin", 'http://jquery-ui.googlecode.com');
        xhr.setRequestHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
    },
    crossDomain: true,
    success: function (data) {
        alert(data);
    }, 
    error: function (jqXHR, textStatus, errorThrown) {
        alert(errorThrown + ' ' + textStatus + ' ' + jqXHR.responseText);
    }
});

感觉我在这里错过了很多......任何见解都会有所帮助。

4 个答案:

答案 0 :(得分:7)

服务器似乎不允许跨域请求。你无能为力。

您可以设置一个可以卷曲该页面的PHP脚本,然后您就可以调用PHP脚本了。像kuncajs所说的那样

您可以在服务器上使用这个简短的PHP curl脚本:

<?php

$ch = curl_init();
// URL to grab
curl_setopt($ch, CURLOPT_URL, 'http://jquery-ui.googlecode.com/svn/tags/1.8.23/themes/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

print_r($output);

?>

然后它只是一个简单的ajax脚本:

$.ajax({
    url: "linktoscript.php",
    dataType: "html",
    success: function(data) {
        console.log(data);
    },
    error: function (request, status, error) {
        console.log(request);
        console.log(status);
        console.log(error);
    }
});

答案 1 :(得分:2)

我从yahoo(YQL)this Cross-domain requests with jQuery plugin找到了使用YQL获取跨域内容的此服务。

http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

DEMO: http://jsfiddle.net/SXHrB/4/

以下代码只是将我解析的整个页面提取给我以获取所需内容。

$.ajax({
    url: 'http://jquery-ui.googlecode.com/svn/tags/1.8.23/themes/',
    type: 'GET',
    success: function(data) {
        alert(data.responseText.substring(data.responseText.indexOf('<ul>'), data.responseText.lastIndexOf('</ul>') + 4));
    }
});

答案 2 :(得分:0)

您是否尝试过使用JQuery使用的theme switcher插件:
它将提供JQuery用于演示目的的所有现成主题。

<script src="jquery.js"></script>
<script type="text/javascript" src="themeSwitcher.js"></script>
<script type="text/javascript">       
   $(document).ready(function(){
       $('#switcher').themeswitcher();
   });
</script>    

<div id="switcher"></div>

答案 3 :(得分:0)

您可以让您的网站添加指向此样式的链接:

当您单击新主题时,javascript会在头部添加链接标记 您可以使用以下任何名称替换链接的ui-lightness部分:http://jquery-ui.googlecode.com/svn/tags/1.8.23/themes/ 希望这有帮助