简短:
XSLT 应用于 XML,我想使用 document(http://...) 加载另一个 XML ,来自不同于 XSL 和原始 XML 的另一个域。我将 CORS header 添加到服务器,它可以在 Firefox 上运行,而不是在 Chrome 上运行。为什么,以及如何解决这个问题?
完整案例:
我首先使用 html5Rocks 示例尝试了 CORS 请求。所以我有一个 html 文档,位于 http://localhost/cors.html包含此代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>
</title>
<script>
function createCORSRequest(method, url)
{
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr)
{
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined")
{
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
}
else
{
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
function go()
{
console.log('go!');
var url = 'http://cors1.localhost/cors-data.xml';
var xhr = createCORSRequest('GET', url);
if (!xhr)
{
throw new Error('CORS not supported');
}
xhr.onload = function()
{
var responseText = xhr.responseText;
var responseXml = xhr.responseXML;
console.log(responseXml);
// process the response.
};
xhr.onerror = function()
{
console.log('There was an error!');
};
xhr.send();
}
document.addEventListener('DOMContentLoaded', go, false);
</script>
</head>
<body>
</body>
</html>
在 firefox 上运行良好:XHR 对象发送 CORS 请求,浏览器和服务器都能很好地处理它,这要归功于以下服务器的 .htaccess 文件:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Credentials "true"
Header set Access-Control-Allow-Methods "OPTIONS, GET, POST"
Header set Access-Control-Allow-Headers "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control"
现在,我在 Chrome 上测试它...没问题,它也能正常工作 ☺
在这两种浏览器中,控制台都会显示 XHR 响应的内容(它的 responseXml ),所以我假设服务器配置良好(不是吗?)。
现在,我有一个 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="CORS.xsl"?>
<cors source="http://cors1.localhost/CORS-data.xml"/>
XSLT 应用于它:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/cors">
<xsl:variable name="cors" select="document(@source)/cors"/>
<p>
<xsl:text>CORS-data.xml (</xsl:text>
<a href="{@source}">
<xsl:value-of select="@source"/>
</a>
<xsl:text>): </xsl:text>
<xsl:value-of select="$cors"/>
</p>
</xsl:template>
</xsl:stylesheet>
因此 XSLT 应该加载外部文档 (http://cors1.localhost/CORS-data.xml) 并显示其内容 (<xsl:value-of select="$cors"/>)。它实际上在 Firefox 中做得很好,但在 Chrome 中却不行,控制台说:
Unsafe attempt to load URL http://cors1.localhost/CORS.xsl from frame with URL http://localhost/CORS.xml. Domains, protocols and ports must match.
页面结果为:
CORS-data.xml (http://cors1.localhost/CORS-data.xml):
: 之后应该有 document() 加载的 XML 内容(一个简单的“ok”)但它是无效的。
我看到了几个关于这类问题的主题,但它们都是关于 file:/// 的协议(protocol),与http://无关.我可以理解 file:///出于安全原因,不允许使用 XSLT,但我不明白
为什么 Chrome 的 CORS 与 javascript 的 XHR 一起工作,但与 XSLT 的 document() 一起失败?功能? 以及如何解决这个问题?
最佳答案
Chrome 不允许本地主机作为来源,您应该尝试使用 --allow-file-access-from-files 和 --disable-web-security 标志启动您的 Chrome。
关于javascript - 带有 Chrome 的 CORS XSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24633079/
使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做
假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而
当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到
我将Cucumber与Ruby结合使用。通过Selenium-Webdriver在Chrome中运行测试时,我想将下载位置更改为测试文件夹而不是用户下载文件夹。我当前的chrome驱动程序是这样设置的:Capybara.default_driver=:seleniumCapybara.register_driver:seleniumdo|app|Capybara::Selenium::Driver.new(app,:browser=>:chrome,desired_capabilities:{'chromeOptions'=>{'args'=>%w{window-size=1920,1
我在一个简单的RailsAPI中有以下Controller代码:classApi::V1::AccountsControllerehead:not_foundendendend问题在于,生成的json具有以下格式:{id:2,name:'Simpleaccount',cash_flows:[{id:1,amount:34.3,description:'simpledescription'},{id:2,amount:1.12,description:'otherdescription'}]}我需要我生成的json是camelCase('cashFlows'而不是'cash_flows'
在Ruby(或Rails)中,我们可以做到new_params=params.merge({:order=>'asc'})现在new_params是一个带有添加键:order的散列。但是是否有一行可以返回带有已删除key的散列?线路new_params=params.delete(:order)不会工作,因为delete方法返回值,仅此而已。我们必须分3步完成吗?tmp_params=paramstmp_params.delete(:order)returntmp_params有没有更好的方法?因为我想做一个new_params=(params[:order].blank?||para
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
如何使用rubyonrails获取网络上某处其他网站的页面数据? 最佳答案 您可以使用httparty只是获取数据示例代码(来自example):requireFile.join(dir,'httparty')require'pp'classGoogleincludeHTTPartyformat:htmlend#google.comredirectstowww.google.comsothisislivetestforredirectionppGoogle.get('http://google.com')puts'','*'*7