2016-03-06 32 views
0

我有在AngularJS如下:消費維基阿比使用AngularJS

(function() { 
    'use strict'; 

    angular 
     .module('ui-chat-app') 
     .factory('wikiService', function($http) { 
    var wikiService = { 
     get: function(keyword) { 
      return $http.jsonp('https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=1&explaintext&exintro&titles=' + keyword.name); 
     } 
    }; 

    return wikiService; 

}) 

.controller('aiChatBrain', function($scope, wikiService) { 
    wikiService.get({name: 'Sarajevo'}).then(function(data) { 
     console.log(data); 
    }); 

}); 

})(); 

當腳本在Firefox執行我收到以下錯誤:

enter image description here

而當腳本在鉻中執行我得到以下錯誤:

enter image description here

任何想法爲什麼發生這種情況? :/我完全困惑...

有人可以幫我解決這個問題嗎?

謝謝堆!

+0

任何人,真的嗎? ._。 – perkes456

+0

嗨,我的回答有幫助嗎?如果我沒有/沒有幫助解決問題,我會享受一些反饋。 – glcheetham

回答

1

當使用JSONP,從服務器的響應被送到一個回調函數和JavaScript的執行(見這裏What is JSONP all about?

因此,使得角JSONP請求時,你需要指定一個回調函數:

The name of the callback should be the string JSON_CALLBACK

docs

正在執行你的JSON字符串,但它本身並不是一個有效的JavaScript語句,因此錯誤。

嘗試使用$http.get而不是它會按預期工作。