从火力地堡通过角托管获取JSONP文件 [英] Obtaining JSONP files from Firebase hosting through Angular

查看:181
本文介绍了从火力地堡通过角托管获取JSONP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得以.json 从远程火力服务器文件。

I am trying to obtain a .json file from remote firebase server.

function fetchData(remoteJsonId){
    var url = "https://myAppName.firebaseapp.com/topics/"+remoteJsonID;
    console.log(url); //This variable expands to the full domain name which is valid and                       returns success both on wget and the browser
    $http.jsonp(url).then(
        function(resp){

        },
        function(err){
            console.log(err.status) // This posts "404" on console.
        }
    );
}

不过,如果我打开在浏览器网址 JSON文件加载。即使我 wget的网址 JSON文件加载。但通过角返回一个 404 未找到。

But If I open url in the browser the json file loads. Even if I wget url the json file loads. But through angular it returns a 404 not found.

现在的以.json 远程文件中有这样的结构:

Now the .json remote file has this structure:

[
  { 
    "hello":"Europe"
  },

  {
    "hello":"USA"
  }
]

上面的文件可以获取的使用$ http.get(),但不能含有$ http.jsonp()。 JSONP斜面解析以.json具有上述结构的文件。我该如何解决此问题?

The above file can be fetched using $http.get() but not with $http.jsonp(). JSONP cant parse .json file with the above structure. How can I work around this?

推荐答案

您需要指定一个?回调= JSON_CALLBACK 在传递给网址 $ http.jsonp

You need to specify a ?callback=JSON_CALLBACK in the URL that you pass to $http.jsonp.

角的 $ http.jsonp 文档:

jsonp(url, [config]);
Shortcut method to perform JSONP request.

Parameters
Param   Type    Details
url     string  
Relative or absolute URL specifying the destination of the request.
The name of the callback should be the string JSON_CALLBACK.

这最后一行是你错过了什么。

That last line is what you're missing.

一个简单的例子(使用火力地堡的数据库,而不是火力地堡主机):

A simple example (using a Firebase database, not Firebase hosting):

var app = angular.module('myapp', []);
app.controller('mycontroller', function($scope, $http) {
  var url = 'https://yourfirebase.firebaseio.com/25564200.json';
  $http.jsonp(url+'?callback=JSON_CALLBACK').then(
    function(resp){
      console.log(resp.data); // your object is in resp.data
    },
    function(err){
        console.error(err.status)
    }
  );  
});

如果你想看到它的工作: http://jsbin.com/robono / 1 /手表?JS,控制台

这篇关于从火力地堡通过角托管获取JSONP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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