让AJAX调用在科尔多瓦应用AngularJS [英] Make AJAX call in AngularJS in Cordova App
问题描述
我已经建立了使用离子型框架,AngularJS和科尔多瓦的应用程序。在这里面,我做了AJAX调用$ http.get()使用多个PHP文件,在浏览器中完美地工作,而不是在应用程序内。该应用程序能够通过一个IFRAME渲染互联网网页使网络运行正常。我有我的白名单服务器的PHP文件驻留使用app.js文件和内部。此外,在我的PHP文件,我已经添加了标题(访问控制 - 允许 - 产地:*');
这是如何得到这个AJAX调用任何建议工作?
.controller(StuffCtrl',函数($范围,StuffService,LoadingService){
StuffService.getStuff()。然后(功能(数据){
LoadingService.show();
$ scope.stuff =数据;
LoadingService.hide();
});
})
。服务(StuffService',函数($ HTTP){
VAR myStuff;
返回 {
getStuff:函数(){
返回$ http.get('HTTP://mydomain/stuff.php)。然后(函数(项目){
myStuff = items.data;返回myStuff;
});
}
});
});
我有同样的错误,而工作的一个混合的应用程序,然后我说我的路由配置功能,这些线路,并开始工作。
$ httpProvider.defaults.useXDomain = TRUE;
删除$ httpProvider.defaults.headers.common ['X-要求,随着'];
I have built an app using Ionic Framework, AngularJS, and Cordova. In it, I have made AJAX calls to several php files using $http.get(), which works perfectly in a browser, but not within the app. The app is able to render internet pages through an iframe so the network is working. I have whitelisted my server where the php files reside inside of the app.js file and using . Also, in my php files I've added header('Access-Control-Allow-Origin: *');
Any suggestions on how to get this AJAX call to work?
.controller('StuffCtrl', function($scope, StuffService, LoadingService) {
StuffService.getStuff().then(function(data) {
LoadingService.show();
$scope.stuff = data;
LoadingService.hide();
});
})
.service('StuffService', function($http){
var myStuff;
return {
getStuff: function(){
return $http.get('http://mydomain/stuff.php').then(function(items) {
myStuff = items.data; return myStuff;
});
}
});
});
I had the same error while working on a hybrid app and then I added these lines in my route config function and it started working
$httpProvider.defaults.useXDomain=true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
This is to enable CORS. More info on the same can be found here
这篇关于让AJAX调用在科尔多瓦应用AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!