如何$ http.jsonp()工作 [英] How does $http.jsonp() work

查看:135
本文介绍了如何$ http.jsonp()工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个以.json 文件的例子:

Here is an example of a .json file:

[
  {
     "name":"Jon Skeets"
  },

  {
     "name":"Bill Joy"
  }
]

如果这个JSON文件作为获得:

If this json file is obtained as:

$http.jsonp(pathToFile).then() {
     ... 
}

它返回一个404没有什么错 pathToFile 我与卷曲,wget和浏览器。但问题是与 JSONP()不必取与上述结构以.json 文件。 $ http.get()可以解析以.json 与上述结构的文件。但 JSONP 着。它需要在顶层的字典,而不是一个数组。

It returns a 404. There is nothing wrong with pathToFile I have verified it with curl, wget and the browser. But the problem is with jsonp() having to fetch .json files with the above structure. $http.get() can parse .json files with the above structure. But JSONP cant. It needs a dictionary at the top level and not an array.

要证明这一点,我已经创造了这个火力点: https://开头炽烈,防火6512.firebaseapp.com/name.json

To demonstrate this, I have created this firebase: https://blazing-fire-6512.firebaseapp.com/name.json

在获取浏览器或wget的链接和它的作品。试着用获取在这里角并返回404请求失败。

fetch the link in your browser or wget and it works. Try fetching it with Angular here and it returns 404 Request failed.

推荐答案

火力地堡主机不支持JSONP。它不处理的回调用来评估跨原点JSONP回调参数。

Firebase hosting does not support JSONP. It doesn't handle the callback parameter that is used to evaluate JSONP callbacks across origins.

你说这个储存在JSON网址 http://example.com/name.json

Say you store this JSON at URL http://example.com/name.json:

{ "name":"Jon Skeets" }

如果您访问的网址 http://example.com/name.json 你回来正是JSON。这是precisely什么 $ http.get 一样。

If you access the URL http://example.com/name.json you get back exactly that JSON. And that is precisely what $http.get does.

当一个服务器支持JSONP,它接受回调参数。在这种情况下,URL结束 http://example.com/name.json?callback=dis​​playName 和反应是:

When a server supports JSONP, it accepts a callback parameter. In such a case the URL ends up http://example.com/name.json?callback=displayName and the response is:

displayName('{ "name":"Jon Skeets" }');

起源各地要检索

这使得JSON数据,这是整个原因JSONP的存在。

This allows JSON data to be retrieved across origins, which is the whole reason for JSONP's existence.

火力地堡的动态数据服务器支持JSON。火力地堡主机没有。

Firebase's dynamic data servers support JSON. Firebase hosting does not.

一些可能的解决方案:


  1. 如果要存储的JSON文件的火力地堡上托管的应用程序,你可以使用 $ http.get

  2. 由于你控制了JSON文件,也可以简单地把一个函数名里面name.json

  3. 在支持JSONP主机存储JSON文件。我经常把他们在Github的要旨。

  4. 存储的数据结构在火力地堡分层数据库,而不是他们的托管服务器上

  1. If you're storing the JSON file and the application on Firebase hosting, you can just use $http.get.
  2. Since you control the JSON file, you can also simply put a function name inside name.json
  3. Store the JSON file at a host that supports JSONP. I often put them in Github Gists.
  4. Store the data structure in a Firebase hierarchical database, and not on their hosting servers

这篇关于如何$ http.jsonp()工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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