作業コードの例以下:
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
})
は、依存関係を注入することを確認します。
あなたはホワイトリストプラグインを使用して、原点のアクセスを許可していますか? – sonu
はい私が使っているのは:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=ja –