我对 angularJs 很陌生,在我的 ionic 应用程序中,我尝试将 json 发送到我的服务器,这是 Controller :
`enter code here`.controller('DashCtrl', ['$scope','$http',function($scope, $http) {
$scope.formData = {};
$scope.sendForm = function(){
$http({
method : 'POST',
url : 'http://www.---.org/appForm.php',
data : JSON.stringify($scope.formData), // pass in data as strings
headers : {'Access-Control-Allow-Origin':'*'}
})
.success(function(data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorName = data.errors.name;
$scope.errorSuperhero = data.errors.superheroAlias;
} else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
};
但是当我发送帖子时,控制台向我显示以下错误消息:
-[错误] 无法加载资源:Access-Control-Allow-Origin 不允许 Origin htt..。 (appForm.php,第 0 行)
-[错误] XMLHttpRequest 无法加载 htt.. Access-Control-Allow-Origin 不允许 Origin http://localhost:8100
。 (本地主机,第 0 行)
我尝试将此 .htaccess 放入我的主文件夹中:
header 添加 Access-Control-Allow-Origin "*" header 添加 Access-Control-Allow-Headers“origin, x-requested-with, content-type” header 添加 Access-Control-Allow-Methods“PUT、GET、POST、DELETE、OPTIONS”
但这会添加另一个错误:
-无法加载资源:服务器响应状态为 500(内部服务器错误)。
我要疯了!有人知道吗?!非常感谢您的关注
最佳答案
您可以将服务代理添加到您的 ionic.project
文件中。
{
"name": "appname",
"email": "",
"app_id": "",
"proxies": [
{
"path": "/v1",
"proxyUrl": "https://api.instagram.com/v1"
}
]
}
关于javascript - Ionic 应用程序和服务器帖子 : Acces-Control-Allow-Origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26165879/