如何使用 Angular $http 服务从 MongoDB REST API 检索简单数据?
How can I use the Angular $http service to retrieve simple data from the MongoDB REST API?
我有一个简单的 MongoDB,用于存储公司范围内可访问的临时虚拟数据。
我可以通过以下方式成功查询到数据:
$http.jsonp("http://my_server/my_database/my_collection/?callback=JSON_CALLBACK&jsonp=angular.callbacks._0")
.then(getServersComplete)
.catch(getServersFailed);
function getServersComplete(response) {
var test = response.data;
//do stuff
}
function getServersFailed(error) {
$log.error('XHR Failed for getServers.\n' + angular.toJson(error.data, true));
}
我的问题是 Mongo 的 REST 接口需要查询参数 jsonp=my_callback
而 Angular 的 $http
服务需要查询参数 callback=JSON_CALLBACK
. Angular 然后将 JSON_CALLBACK
翻译成它自己的函数,在本例中是 angular.callbacks._0
(但如果页面上有更多的回调,它将是 angular.callbacks._1
、angular.callbacks._2
, ETC。)。我如何知道 Mongo 动态创建了哪个回调 Angular?
我按照此处所述实施了 jsonpInterceptor
:
How to Custom Set AngularJS JSONP Callback Name
.factory('jsonpInterceptor', function($timeout, $window, $q) {
return {
'request': function(config) {
if (config.method === 'JSONP') {
var callbackId = angular.callbacks.counter.toString(36);
config.callbackName = 'angular_callbacks_' + callbackId;
config.url = config.url.replace('JSON_CALLBACK', config.callbackName);
$timeout(function() {
$window[config.callbackName] = angular.callbacks['_' + callbackId];
}, 0, false);
}
return config;
},
'response': function(response) {
var config = response.config;
if (config.method === 'JSONP') {
delete $window[config.callbackName]; // cleanup
}
return response;
},
'responseError': function(rejection) {
var config = rejection.config;
if (config.method === 'JSONP') {
delete $window[config.callbackName]; // cleanup
}
return $q.reject(rejection);
}
};
})
我有一个简单的 MongoDB,用于存储公司范围内可访问的临时虚拟数据。
我可以通过以下方式成功查询到数据:
$http.jsonp("http://my_server/my_database/my_collection/?callback=JSON_CALLBACK&jsonp=angular.callbacks._0")
.then(getServersComplete)
.catch(getServersFailed);
function getServersComplete(response) {
var test = response.data;
//do stuff
}
function getServersFailed(error) {
$log.error('XHR Failed for getServers.\n' + angular.toJson(error.data, true));
}
我的问题是 Mongo 的 REST 接口需要查询参数 jsonp=my_callback
而 Angular 的 $http
服务需要查询参数 callback=JSON_CALLBACK
. Angular 然后将 JSON_CALLBACK
翻译成它自己的函数,在本例中是 angular.callbacks._0
(但如果页面上有更多的回调,它将是 angular.callbacks._1
、angular.callbacks._2
, ETC。)。我如何知道 Mongo 动态创建了哪个回调 Angular?
我按照此处所述实施了 jsonpInterceptor
:
How to Custom Set AngularJS JSONP Callback Name
.factory('jsonpInterceptor', function($timeout, $window, $q) {
return {
'request': function(config) {
if (config.method === 'JSONP') {
var callbackId = angular.callbacks.counter.toString(36);
config.callbackName = 'angular_callbacks_' + callbackId;
config.url = config.url.replace('JSON_CALLBACK', config.callbackName);
$timeout(function() {
$window[config.callbackName] = angular.callbacks['_' + callbackId];
}, 0, false);
}
return config;
},
'response': function(response) {
var config = response.config;
if (config.method === 'JSONP') {
delete $window[config.callbackName]; // cleanup
}
return response;
},
'responseError': function(rejection) {
var config = rejection.config;
if (config.method === 'JSONP') {
delete $window[config.callbackName]; // cleanup
}
return $q.reject(rejection);
}
};
})