简体   繁体   English

从html页面提供javascript文件

[英]Serve javascript file from an html page

I have a resource from a third party vendor that serves through http protocol only. 我有一个第三方供应商的资源,该资源仅通过http协议提供。 When I include a script tag like this... 当我包含这样的脚本标签时...

<script src="http://example.com"></script>

I get a javascript variable with the JSON data, similar to this... 我得到一个带有JSON数据的JavaScript变量,类似于此...

var data = {"total": "10", "results": [{"name": "Joe", "title": "developer"}, {"name": "Jane", "title": "engineer"}]};

I can then use the variable data in my page to output results, etc. 然后,我可以使用页面中的变量data来输出结果,等等。

The problem is, I need to serve this data on a page over SSL ( https ) and I cannot make a resource request through http for an https page - meaning the <script/> include does not work. 问题是,我需要通过SSL( https )在页面上提供此数据,并且我无法通过http来请求https页面的资源-意味着<script/> include不起作用。

As a solution I am requesting the resource from a server page, lets say data.cfm for example, then re-serving the JS variable through https from my server. 作为一种解决方案,我从服务器页面请求资源,例如说data.cfm ,然后从我的服务器通过https重新data.cfm JS变量。 So, in this case, I'd like a request to https://anotherserver.com/data.cfm to serve the same JS as http://example.com 因此,在这种情况下,我想请求https://anotherserver.com/data.cfm来提供与http://example.com相同的JS

<cfhttp result="myData" method="GET" charset="utf-8" url="http://example.com">
<cfoutput><pre>#myData.fileContent#</pre></cfoutput>

I should also note that the JSON data has HTML tags in it. 我还要注意,JSON数据中包含HTML标记。

When I try to go to https://anotherserver.com/data.cfm I get the data, but it is not formatted correctly. 当我尝试访问https://anotherserver.com/data.cfm我得到了数据,但是格式不正确。 For example, hyperlinks are active as hyperlinks. 例如,超链接被激活为超链接。

I also tried using <cfdump> which returns the data as an unformatted string, but when I use the <script src="https://anotherserver.com/data.cfm"></script> tag I do not get the JS variable. 我还尝试使用<cfdump>将该数据作为未格式化的字符串返回,但是当我使用<script src="https://anotherserver.com/data.cfm"></script>标记时,我没有得到JS变量。

Update 更新资料

With further testing, I found that if I just copy the data as text in 2 files: data.cfm and data.js the js file works as expected and the cfm file does not. 经过进一步的测试,我发现如果仅将数据作为文本复制到2个文件中:data.cfm和data.js,则js文件将按预期工作,而cfm文件则不会。 This leads me to believe that the file extension is causing the resource to be read as html. 这使我相信文件扩展名导致资源被读取为html。 So, it's really more of a Coldfusion question. 因此,实际上更多的是Coldfusion问题。 How can I set the cfm file to be read as javascript by the browser? 如何将cfm文件设置为浏览器读取为javascript? Is there some response header or metadata field that will accomplish this? 是否有一些响应标头或元数据字段可以完成此任务?

I'll probably get creamed in negative votes for this. 为此,我可能会被否决。 The correct thing to happen is you tell that service to enable https on their servers. 正确的事情是您告诉该服务在其服务器上启用https。 A public facing web based service not using https is not acceptable. 不使用https的面向公众的基于Web的服务是不可接受的。

First of all if you are viewing the js and cfm files in the browser then js is going to show essentially as text output and cfm will be interpreted as html. 首先,如果您正在浏览器中查看js和cfm文件,则js实质上将显示为文本输出,而cfm将被解释为html。 This is due to mime types returned automatically by the web server or guessing done by the browser. 这是由于Web服务器自动返回的mime类型或浏览器的猜测造成的。 This is why the cfm method looks different. 这就是cfm方法看起来不同的原因。 However if you are pulling a url using a script tag it will be assumed to be javascript. 但是,如果您使用脚本标签提取网址,则将其假定为javascript。 If you do source code view on the cfm page it should look fine. 如果您在cfm页面上查看源代码,它应该看起来不错。

Secondly we would need more information to diagnose exactly what is causing the problem you are experiencing where a js file included vs a cfm file hosting the same info is resulting an error in the latter. 其次,我们将需要更多信息来准确诊断导致您遇到的问题的原因,其中包含的js文件与包含相同信息的cfm文件在后者中导致错误。 My initial guess would be that more is going on here than we can see in your question and there is a javascript escaping issue. 我最初的猜测是,这里发生的事情比我们在您的问题中看到的要多,并且还有一个javascript逃避问题。 (edit: actually I see that some js code may make decisions based on content type so returning that could be the difference depending on how things are used) (编辑:实际上,我看到一些js代码可能根据内容类型做出决定,因此返回值可能取决于使用方式而有所不同)

Lastly, if you save exact output of the cfm file to a js file then you should be set. 最后,如果将cfm文件的确切输出保存到js文件,则应进行设置。 Pulling javascript from cfm file by way of script tags should have no issue. 通过脚本标记从cfm文件中提取javascript应该没有问题。 However to avoid any potential issues and also to add some caching you can save right to a static file. 但是,为避免任何潜在的问题并添加一些缓存,您可以将权限保存到静态文件中。 Caching will allow the file to only ever be pulled once in a time period (ex: once a minute). 缓存将允许在一个时间段内仅拉一次文件(例如:每分钟一次)。 In your normal code you could add this... 在您的普通代码中,您可以添加此...

<cfif not structKeyExists(application, "jsfilename_cachesaved") or application.jsfilename_cachesaved lt dateAdd("n", -1, now())>
    <cflock name="jsfileupdate" timeout="5">
        <cfif not structKeyExists(application, "jsfilename_cachesaved") or application.jsfilename_cachesaved lt dateAdd("n", -1, now())>
            <cfhttp result="myData" method="GET" charset="utf-8" url="http://example.com">
            <cffile action="write" file="#jsfilepath#" output="myData.fileContent" />
            <cfset application.jsfilename_cachesaved = now() />
        </cfif>
    </cflock>
</cfif>

(the double if statement is there to avoid unnecessary hits to lock and also unnecessary saves for users who hit the lock at the same time) (使用double if语句可以避免不必要的点击锁定,也可以避免同时点击锁定的用户节省不必要的费用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM