【发布时间】:2016-02-04 22:07:01
【问题描述】:
我正在使用 phonegap 和 Ionic 开发一个应用程序,但在使用 HTTPS (SSL) 时遇到了困难。我对为什么这拒绝工作感到困惑。 它在 Firefox 上就像一个魅力,但是当我在我的手机上运行它时它不起作用。
如果我使用普通 HTTP,它可以正常工作,但 HTTPS 不能,我认为它与用于 SSL 的端口 443 有关,但不知道如何在我的智能手机 (android) 上检查它。
问题: 为什么 HTTPS 在我的智能手机上不起作用,我该如何让它起作用?
登录代码:
$scope.login = function(name, password) {
$scope.type = 'login';
// Data to be sent to the database
var data = JSON.stringify({
type: $scope.type,
name: name.toLowerCase(),
password: password
});
var useSSL = false;
// Call httpPost from app.js with this scope and the data to be inserted
$scope.httpPost($scope, data, $scope.type, useSSL);
};
功能:httpPost
$rootScope.httpPost = function(scope, question, type, SSL) {
var urlBase = "http";
if (SSL) urlBase = "https";
var request = $http({
method: "post",
url: urlBase+"://mydomain.xyz/myproject/api.php",
crossDomain : true,
data: question,
headers: { 'Content-Type': 'application/json' }
});
/* Successful HTTP post request or not */
request.success(function(data) {
if (type == "login"){
// Confirm it's a valid token
if(data.length == 10){
showAlert('confirmation', 'you are now online');
}
else{
showAlert('Try again', 'wrong credentials');
}
}
})
.catch(function(data, status, headers, config) {
// No response from server
// This is what triggers and the data returned is useless
showAlert('Ooops', JSON.stringify(data));
});
}
服务器端
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: X-PINGOTHER');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve the incoming data
$postdata = file_get_contents("php://input");
// Check if the data is defined
if (isset($postdata)) {
// Parse the incoming data
$request = json_decode($postdata);
// Connect to database
include 'connect.php';
$db = Database::getInstance();
$db->connect();
// Login attempt and non-empty parameters
if ($request->type == "login" && $request->name != "" && $request->password != "") {
// Verify if the password matches
// Set token
// Execute the SQL
// Return the token
}
echo "Missing parameters!";
}
}
?>
如果你不管怎样,想看看返回了什么。我这里有一个例子(不同的操作,这是一个删除操作而不是登录但仍然是相同的结果)
编辑: 刚刚审查了一些链接.. 显然有人来自法国试图访问我服务器上不存在的文件夹-.-
【问题讨论】:
-
有人否决了我的问题。我问这个问题是不是做错了什么?如果是这种情况,请告诉我,以便我进行更正。
-
DELETE 不在您允许的方法中,因此它会给出 CORS 错误(Angular 中的状态 0)
-
"delete" 只是我作为变量连同我的数据一起发送的一个参数,它与我发送的方法或任何东西无关。
标签: javascript php android angularjs https