我如何在我的AngularJS应用程序上设置.htaccess,以防止出现以下错误消息:
“无法加载https://api.mailgun.net/v3/example.com/messages:请求标题字段Authorization未被预检响应中的Access-Control-Allow-Headers允许。”
这是我的.htaccess文件:
我在网站这里收到错误信息,每当用户尝试提交联系表单时。
以下是处理过程的代码:
<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "Content-Type, Authorization"
Header add Access-Control-Allow-Methods "GET, POST"
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html
</IfModule>
我在网站这里收到错误信息,每当用户尝试提交联系表单时。
以下是处理过程的代码:
function send(apiUrl, from, to, subject, body, apiKey) {
var url = apiUrl;
var dataJSON = {
from: from,
to: to,
subject: subject,
text: body,
multipart: true
};
var req = {
method: 'POST',
url: url,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + $base64.encode('api:'+apiKey)
},
transformRequest: function (obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
}
return str.join('&');
},
data: dataJSON
};
$http(req).then(function (data) {
if(data.data) {
if(data.data.message === 'Queued. Thank you.') {
$window.alert('Sent. Thank you.');
}
else {
$window.alert(data.data.message);
}
}
else {
$window.alert('An error has occured. Please try again.');
}
}, function (data) {
if(data.data) {
if(data.data.message === 'Queued. Thank you.') {
$window.alert('Sent. Thank you.');
}
else {
$window.alert(data.data.message);
} }
else {
$window.alert('An error has occured. Please try again.');
}
});
}