javascript - 使用 require.js 进行跨域调用时出现问题?

标签 javascript jquery requirejs cross-domain

我想将 HTML div 从托管在 windows azure blob(我自己的)上的 html 文件加载到同样托管在 windows azure web Angular 色上的 MVC View (blob 和 web Angular 色都归我所有) .

我尝试使用 jquery.load(),但它遇到了跨域问题,并且窗口 azure blob 不允许更改 CORS 策略。接下来我尝试使用Require.JsText.Js来实现这种跨域负载。 来自 Text.Js 文档:

Text plugin determines that the request for the resource is on another domain, it will try to access a ".js" version of the resource by using a script tag. Script tag GET requests are allowed across domains. The .js version of the resource should just be a script with a define() call in it that returns a string for the module value.

我使用 require.js 进行跨域调用的 js 代码是:

require(["text!http://xxxxx.blob.core.windows.net/xxx/File"],
 function (html) {
   alert(html);
 }
);

现在我面临的问题是文件的 .js 版本已由插件成功加载(通过使用 chrome 检查器确认),但在回调中,当我尝试访问以下文本内容时文件,它显示未定义。我在这里做错了什么,为什么回调没有文本内容?有人可以帮我解决这个问题吗?如果这是无法解决的,我愿意接受其他方法来实现这种跨域负载。

我对 require.js 文档的理解是,跨域调用可以通过调用资源的 javascript 版本来完成。

最佳答案

尝试使用定义:

define(["text!http://xxxxx.blob.core.windows.net/xxx/File"], function (html) {
   return {
     template: html;
   }:
 }
);

关于javascript - 使用 require.js 进行跨域调用时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173476/

相关文章:

javascript - 添加过渡到 jQuery 样式更改

jQuery addClass() 与 Bootstrap

javascript - 将纯 RequireJS 项目移至 Maven

testing - 使用 Angularjs 和 requirejs 时在测试中注入(inject)

backbone.js - 非常基本的 Backbone/Underscore 通过 Require.js 问题驱动我

没有调用 Javascript 函数 onblur

javascript - 使用 Ember.js 将新行动态插入表上下文

php - 使用 PHP 运行查询并将其放入 JavaScript 对象中

javascript - 获取托管 webapp 的服务器的外部 IP

javascript - 如何更改此翻转卡以在单击时自行旋转?