javascript - 如何在 JavaScript 中获取 .js 脚本的源代码?

标签 javascript jquery ajax

$.ajax({url: 'http://gmaps-samples-v3.googlecode.com/svn-history/r16/trunk/xmlparsing/util.js', dataType: 'script text text', crossdomain:'true', success: function(msg, status, obj){console.log(msg);console.log(status);console.log(obj)}, mimetype: 'text/plain', cache:false});

我尝试运行上面的代码及其变体 - 删除 mimetype、缓存、将 dataType 设置为“脚本文本”和“脚本脚本文本”。

直接来自 jQuery 文档:

multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml

我只能发出“script”类型的 dataType 请求,否则我会收到“...is not allowed by Access-Control-Allow-Origin”错误。但无论如何,我不应该可以在 jQuery 中自由地解释它吗?我已经明确要求将其作为文本,但无论我做什么,msg(从服务器返回的数据)始终是“未定义”。

有没有解决这个问题的方法,不管是否有问题?

编辑:此代码工作是因为它加载 JavaScript 文件并将其下载到用户的浏览器。但我还是看不到!

最佳答案

But by all rights, shouldn't I be free to interpret it within jQuery however I want to?

浏览器中的安全机制会阻止您执行此操作,因为这会允许您从其他网站窃取用户的私有(private)信息。如果您向脚本所在的同一域发出请求,它将起作用。否则,您无法在 JavaScript 中发出请求,而需要从服务器发出请求。

关于javascript - 如何在 JavaScript 中获取 .js 脚本的源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6301098/

相关文章:

javascript - 如何通过 JavaScript 设置表单操作?

javascript - Vue 后期模型初始化

javascript - 根据选定的单选按钮隐藏显示

javascript - 从 jQuery Ajax 调用 .ashx

Ajax 使用 <g :remoteLink> in Grails

javascript - 如何在 phonegap Windows Phone 中从 javascript 调用 c# 代码

javascript - 所选选项的不透明度

php - Ajax 安全(我希望)

javascript - 按回车键触发 JavaScript,无需刷新网页即可实现多个搜索/AJAX 查询/数据表显示

Javascript:模块模式与构造函数/原型(prototype)模式?