下面带有Ionic V1的工作代码示例:
在您的 WordPress API 文件中添加标题:
header('Access-Control-Allow-Origin: *');
header( 'Access-Control-Allow-Headers: Authorization, Content-Type' );
您的Factory.js 文件代码:
starter.factory('CommonFactory', function($http) {
var base= 'your api file url';
return {
//with POST data method
login_function: function(user, pass) {
return $http({
url: base,
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
data: postData,
});
},
// With GET data method
login_function: function(user, pass) {
return $http({
url: base+ '?username='+user+'&password='+pass,
method: 'GET',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
});
},
}
});
您可以根据需要更改或重写它。
Controller.js
CommonFactory.login_function($scope.user,$scope.pass)
.success(function(res) {
// success handling
})
.error(function(result) {
//error handling
})
确保注入依赖项。