无法使用AJAX将脚本加载为纯文本 [英] Can't load script as plain text with AJAX

查看:97
本文介绍了无法使用AJAX将脚本加载为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上有一个javascript文件,无法修改.

这是我必须下载的脚本示例:

var tags = '';
tags += '<a href="#somelink"><img src="someimage.gif"/></a>;
document.write(tags);

我开始通过AJAX下载脚本并执行该脚本,但遇到了"document.write无法在异步调用中执行"问题.

因此,我想以纯文本格式下载脚本,并从响应中获取所需内容,然后将其放在html页面中应放置的位置,而无需修改原始脚本.

$.ajax({
    type: "GET",
    url: "http://myurlexample.com",
    dataType: "text",
}).success(function(msg){
    console && console.log("The script was downloaded as text: "+msg);
}).error(function(object,status,errortxt){
    console && console.log("The script wasn't downloaded as text. The error:"+ errortxt);
});

但是,当我使用dataType = "text"进行下载请求时,AJAX会引发错误.有什么方法可以解决这个问题,并可以将其实际下载为文本吗?

PS:该脚本适用于Firefox OS特权应用,因此我无法将脚本直接放在html页面中,因为安全CSP不允许使用该脚本(解决方案

由于您似乎能够以某种方式成功运行 脚本,因此这是一个可行的可怕想法:覆盖document.write.

在运行脚本之前,请执行以下操作:

document.write = function(msg) {
    handleTagStringInApp(msg);
    delete document.write; // revert to original document.write when done
};
// now load execute the script...

其中handleTagStringInApp是您编写的以某种方式处理标签字符串的函数.这基本上是JSONP,但是您不能将回调名称调整得不显眼或有用,而必须使用回调名称document.write.

请注意,如果您应用中的其他任何内容实际上需要使用document.write,则这将非常糟糕. (您可以通过保留对实际document.write的引用来解决此问题,例如,在页面加载时使用var realDocWrite = document.write;并使用realDocWrite.call(document, "whatever")调用它.)

I have a javascript file in a server I can't modify.

Here is a sample of the script I have to download:

var tags = '';
tags += '<a href="#somelink"><img src="someimage.gif"/></a>;
document.write(tags);

I started downloading the script via AJAX and executing it, but I bumped into the "document.write cannot be executed in asynchronous call" problem.

So I wanted to download the script as a plain text and take what I need from the response and put it where it should go in my html page without modyfing the original script.

$.ajax({
    type: "GET",
    url: "http://myurlexample.com",
    dataType: "text",
}).success(function(msg){
    console && console.log("The script was downloaded as text: "+msg);
}).error(function(object,status,errortxt){
    console && console.log("The script wasn't downloaded as text. The error:"+ errortxt);
});

But AJAX throws an error when I do the download request using dataType = "text". Is there any way to go around this and actually download it as a text?

P.S: The script is for a Firefox OS privileged app, so I can't put the script directly in the html page because the security CSP doesn't allow it (https://developer.mozilla.org/en-US/Apps/CSP).

解决方案

Since you seem able to run the script successfully somehow, here's a terrible idea that might work: overwrite document.write.

Before you run the script, do:

document.write = function(msg) {
    handleTagStringInApp(msg);
    delete document.write; // revert to original document.write when done
};
// now load execute the script...

where handleTagStringInApp is a function you write that processes the tag string somehow. This is basicallyJSONP, but you can't adjust the callback name to be something unobtrusive or helpful and must instead use the callback name document.write.

Note that this will be really bad if anything else in your app actually needs to use document.write. (You might get around this in your own code by keeping a reference to the real document.write, e.g., using var realDocWrite = document.write; on page load and calling it with realDocWrite.call(document, "whatever").)

这篇关于无法使用AJAX将脚本加载为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆