问题描述
我想使用jquery获取文件的内容,通常我可以使用:
I want to get content of a file using jquery, normally i can use:
$.get("file", function(data) { alert(data) }
当我尝试获取javascript文件时,jquery在返回回调之前运行javascript代码.
When i try to get javascript file, jquery run the javascript code before returning callback.
如何在不运行文件代码的情况下获取文件内容?
How to get file content without running its code?
推荐答案
将dataType
参数设置为$.get()
到"text"
,以告诉jQuery数据是字符串,并且不应该猜测类型. >
Set the dataType
argument to $.get()
to "text"
to tell jQuery that the data is a string and it should not guess the type.
$.get("file", function(data) { alert(data) }, "text");
$.get()
只是$.ajax()
的快捷方式,您可以在 $.ajax()
文档中查看表示dataType
参数具有许多可以传递给ajax调用的值.默认值是一个智能猜测,它可能会发现这是一个脚本,因此可以执行它.可能性之一是"text"
,听起来像您想要的.
$.get()
is just a shortcut for $.ajax()
and you can see in the $.ajax()
documentation that the dataType
argument has a number of values you can pass to the ajax call. The default is an intelligent guess which probably figures out that it's a script so it executes it. One of the possibilities is "text"
which sounds like what you want.
这篇关于jQuery在不运行的情况下获取javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!