对文件发出 json/jsonp xhr 请求:协议

发布于 2024-10-10 08:23:42 字数 271 浏览 9 评论 0原文

我正在编写一个 javascript 应用程序,它将托管在 file: 协议上(即:该应用程序只是位于我硬盘驱动器上某个位置的 html、css 和 javascript 的文件夹)。当我尝试正常的 XHR 请求时,它们由于同源策略问题而失败。

所以我的问题是,如上所述,使用应用程序请求 json/jsonp 文件的最佳方式是什么?

注意:到目前为止,我已经使用硬编码回调函数获得了所有 jsonp 文件,但我希望能够对这些请求使用动态回调函数。有没有办法做到这一点?

I'm writing a javascript app that will be hosted on a file: protocol (ie: the application is just a folder of html, css, and javascript sitting someplace on my hard drive). When I try normal XHR requests they fail because of the same origin policy afaict.

So my question is this, what's the best way to request json/jsonp files with an app as described above?

Note: So far I've got all of my jsonp files using a hard-coded callback functions, but I'd like to be able to use dynamic callback functions for these requests.. is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-10-17 08:23:42

这是一种棘手的工作,但它会给你带来动态回调。基本上,它依赖于 file: 传输速度相当快的事实。它建立一个请求队列并一次发送一个请求。这是我能找到的确保可以链接正确的响应和回调(以保证的顺序)的唯一方法。希望有人能想出更好的方法,但在无法动态生成响应的情况下,这是我能做的最好的事情。

var JSONP = {
  queue: [],
  load: function(file, callback, scope) {
      var head = document.getElementsByTagName('head')[0];
      var script = document.createElement('script');
      script.type = "text/javascript";
      script.src = file;
      head.appendChild(script);
  },

  request: function(file, callback, scope) {
      this.queue.push(arguments);
      if (this.queue.length == 1) {
          this.next();
      }
  },

  response: function(json) {
      var requestArgs = this.queue.shift();
      var file = requestArgs[0];
      var callback = requestArgs[1];
      var scope = requestArgs[2] || this;
      callback.call(scope, json, file);

      this.next();
  },

  next: function() {
      if (this.queue.length) {
          var nextArgs = this.queue[0];
          this.load.apply(this, nextArgs);
      }
  }

};

这就是我测试

window.onload = function() {
  JSONP.request('data.js', function(json, file) { alert("1 " + json.message); });
  JSONP.request('data.js', function(json, file) { alert("2 " + json.message); });
}

Data.js所做的

JSONP.response({
  message: 'hello'
});

This is kind of a hatchet job, but it will get you your dynamic callbacks. Basically it counts on the fact that file: transfers will be pretty fast. It sets up a queue of requests and sends them out one at a time. That was the only way I could figure out to make sure that the correct response and callback could be linked (in a guaranteed order). Hopefully someone can come up with a better way, but without being able to dynamically generate the responses, this is the best I can do.

var JSONP = {
  queue: [],
  load: function(file, callback, scope) {
      var head = document.getElementsByTagName('head')[0];
      var script = document.createElement('script');
      script.type = "text/javascript";
      script.src = file;
      head.appendChild(script);
  },

  request: function(file, callback, scope) {
      this.queue.push(arguments);
      if (this.queue.length == 1) {
          this.next();
      }
  },

  response: function(json) {
      var requestArgs = this.queue.shift();
      var file = requestArgs[0];
      var callback = requestArgs[1];
      var scope = requestArgs[2] || this;
      callback.call(scope, json, file);

      this.next();
  },

  next: function() {
      if (this.queue.length) {
          var nextArgs = this.queue[0];
          this.load.apply(this, nextArgs);
      }
  }

};

This is what I did to test

window.onload = function() {
  JSONP.request('data.js', function(json, file) { alert("1 " + json.message); });
  JSONP.request('data.js', function(json, file) { alert("2 " + json.message); });
}

Data.js

JSONP.response({
  message: 'hello'
});
拥抱我好吗 2024-10-17 08:23:42

出于安全原因,Chrome 对从 file:// url 进行 ajax 调用有非常严格的限制。他们知道这会破坏本地运行的应用程序,并且

Ajax 在 Firefox 中可以很好地处理文件 url,只需注意返回代码不是 http 状态代码;即,0 表示成功,而不是 200-299 + 304。IE

处理这些安全问题的方式与 Chrome 和 Firefox 不同,我希望其他浏览器都有自己的方法。 Web 和桌面应用程序之间的边界是一个非常有问题的领域。

Chrome has very tight restrictions on making ajax calls from a file:// url, for security reasons. They know it breaks apps that run locally, and there's been a lot of debate about alternatives, but that's how it stands today.

Ajax works fine from file urls in Firefox, just be aware that the return code is not an http status code; i.e., 0 is success, not 200-299 + 304.

IE handles these security concerns differently from both Chrome and Firefox, and I'd expect other browsers to each have their own approaches. The border between web and desktop apps is very problematic territory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文