我目前正在开发一个带有Ionic的移动应用程序,虽然我使用我的登录系统阻止了..目的是发送用户通过POST输入的信息,然后是" API"我的网站检查所有并转过结果
但我有以下错误:
XMLHttpRequest cannot load http://myfinance.byethost11.com/MyFinanceAPI/index.php. Origin http://localhost:8100 is not allowed by Access-Control-Allow-Origin.
然而,我试图在跨域搜索信息,但无法运行它...我认为它来自我的服务器配置或api ..但我不知道如果你有解决方案怎么办?< / p>
我的离子控制器:
controller('LoginCtrl', function($scope, $http, $log,$state){
$scope.user = {};
$scope.login = function() {
var request = {
method: "post",
url: "http://myfinance.byethost11.com/MyFinanceAPI/index.php",
data: {
user: $scope.user.username,
password: $scope.user.password
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
$http(request).then(function (response){
if (response.data == '1'){
alert('Bienvenue !');
$state.go('home');
}
else {
alert('Connexion impossible');
}
})
}
})
我的API:
<?php
header("Access-Control-Allow-Origin: *");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user = $request->user;
$password = $request->password;
if($user == 'test'){
echo '1';
}else{
echo '0';
}
提前致谢:)
答案 0 :(得分:0)
尝试一下,让我知道它是否有效。
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
echo "You have CORS!";