Javascript 通过Angular从Firebase主机获取JSONP文件

Javascript 通过Angular从Firebase主机获取JSONP文件,javascript,json,angularjs,firebase-hosting,Javascript,Json,Angularjs,Firebase Hosting,我正在尝试从远程firebase服务器获取.json文件 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 retu

我正在尝试从远程firebase服务器获取
.json
文件

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.
        }
    );
}
但是如果我在浏览器中打开
url
,就会加载json文件。即使我
wget url
加载json文件。但是通过angular,它返回一个
404
notfound

现在,
.json
远程文件具有以下结构:

[
  { 
    "hello":"Europe"
  },

  {
    "hello":"USA"
  }
]

可以使用$http.get()获取上述文件,但不能使用$http.jsonp()。JSONP无法解析具有上述结构的.json文件。如何解决这个问题?

您需要在传递给
$http.jsonp
的URL中指定一个
?callback=JSON\u callback

发件人:

最后一行是你错过的

一个简单的示例(使用Firebase数据库,而不是Firebase主机):


如果您希望看到它工作:

尝试
$http.jsonp('http://myFireBaseApp.com/MyFolder/JSONFile)。然后
请查看更新的链接。您编辑并删除了该链接。你能提供一个真实的链接吗(如果你觉得有必要的话,只需将你的Firebase名字删掉即可)?你能检查一下浏览器的控制台,看看它最终调用了什么URL吗?@FrankvanPuffelen我确实检查过我的浏览器最终调用了什么。它是firebase中json文件的正确链接。请帮忙!我被卡住了。相关:它仍然返回404。到底发生了什么?您通过jsbin为firebase提供的链接起作用了。但为什么不是我的呢?在firebase上托管JSON文件需要某种独特的设置。您没有共享您的
remoteJsonId
,因此我不能确定。但我高度怀疑您只是忘记了在URL的末尾添加
?callback=JSON_callback
,这就是我回答的第一行所说的。为了让你的代码正常工作,我什么也没做。如果这不能解决问题,我建议您设置一个fiddle/bin来重现您的问题。请参见编辑。现在有一个新问题。如果有一个新问题,那么我的答案解决了原来的问题,您需要通过单击答案左侧的复选标记来接受答案。看见
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.
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)
    }
  );  
});