を取得するために私は自分のコードをより再利用可能にするためにしようとしていますが、$http
呼び出しを行う際に問題に遭遇してきた、私は私の通常の方法を使用する場合:
vm.loginUser = function() {
var userData = {
username: vm.userName,
password: vm.userPassword,
grant_type: "password",
client_id: "E0..."
};
console.log('userData: ', userData);
var config = {
headers: {"Content-Type": "application/x-www-form-urlencoded"},
transformRequest: function (data) {
var str = [];
for (var d in data)
if (data.hasOwnProperty(d)) str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
return str.join("&");
}
};
console.log('config: ', config);
$http.post('https://a.n.org.uk/token', userData, config)
.then(function (response) {
console.log('Button clicked: ', response);
}, function (response) {
console.log(response.data.error_description);
}, function (response) {
console.log(response.data);
});
をこれが正常に動作し、私に何の問題も与えません。私はfactory
を作りましたが、それは基本的には同じですが、コンソールでエラーOPTIONS
とエラーが出るようには見えません。preflight check... No 'Access-Control-Allow-Origin' header'
私はこれが単純な修正かどうか疑問に思っています。私Ctrl page
で
factory("homeResource", function ($http, $q) {
return {
getUser: getUser
};
function getUser(userData) {
var request = $http({
method: "post",
url: "https://a.n.org.uk/token",
data: userData,
config: {
headers: {"Content-Type": "application/x-www-form-urlencoded"},
transformRequest: function (data) {
var str = [];
for (var d in data)
if (data.hasOwnProperty(d)) str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
return str.join("&");
}
}
});
return (request.then(handleSuccess, handleError));
}
function handleSuccess(response) {
return (response.data);
}
は、私は私のhomeResource
に渡すので、homeResource.getUser(userData).then(function (res) {console.log(res);});
のようにそれを呼び出すと述べたエラーが発生します。この仕事をする方法はありますか?
どのようなエラーが表示されますか(コンソールを確認してください)? –