這是我的第一個cordova項目。我的http請求不使用POST處理本地php文件。當PHP文件託管在服務器上時,代碼正常工作並正確處理它。但是,當php文件不在本地時。直接在服務器上編輯文件是一件麻煩事,需要花費很多時間,而不僅僅是在本地進行。我想知道這個解決方案,因爲它確實會使開發更容易。Cordova - http請求不處理本地php
這裏是JavaScript代碼
$(document).ready(function() {
$("input").on("focus", function (e) {
e.preventDefault();
e.stopPropagation();
window.scrollTo(0,0);
});
$("#loginBtn").click(function() {
swal({
title: "Logging in ...",
imageUrl: "../images/ring-alt.gif",
showConfirmButton: false,
});
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username != "") {
if (password != "") {
$.post("../php/login.php",
{
username: username,
password: password
},
function (data, status) {
alert(data);
});
} else {
sweetAlert("Oops...", "Please enter your password!", "error");
}
} else {
sweetAlert("Oops...", "Please enter your username!", "error");
}
});
});
警報(數據)顯示整個PHP文件,而不是處理它。而當它被遠程訪問時工作正常。
這裏是工作的js代碼(遠程/託管的PHP文件)
$(document).ready(function() {
$("input").on("focus", function (e) {
e.preventDefault();
e.stopPropagation();
window.scrollTo(0,0);
});
$("#loginBtn").click(function() {
swal({
title: "Logging in ...",
imageUrl: "../images/ring-alt.gif",
showConfirmButton: false,
});
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username != "") {
if (password != "") {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText == "successsuccess") {
window.location.href = '../html/dashboard.html';
} else {
sweetAlert("Oops...", "Invalid username or password!", "error");
document.getElementById("password").value = "";
}
}
}
xmlhttp.open("POST", "http://www.sampleee.esy.es/login.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("username=" + username + "&" + "password=" + password);
} else {
sweetAlert("Oops...", "Please enter your password!", "error");
}
} else {
sweetAlert("Oops...", "Please enter your username!", "error");
}
});
});
現在這裏是php文件
<?php
include("connectdb.php");
$username = mysqli_escape_string($conn, $_POST['username']);
$password = mysqli_escape_string($conn, $_POST['password']);
$password = md5($password);
$getLogin = "SELECT id FROM user WHERE username = '$username' and password = '$password'";
$result = mysqli_query($conn, $getLogin);
if(!$result){
echo "Query Error: ". mysqli_error($conn);
die();
}
$count = mysqli_num_rows($result);
if($count == 1){
echo "success";
}else{
echo "failed";
}
?>
PHP代碼在任何環境下都不起作用;它需要由具有PHP功能的服務器實際處理。對於本地開發,請安裝像xampp這樣的PHP開發環境,並將您的AJAX請求發送到'localhost'。 –
按照@ChrisG的建議:建立一個適當的開發/生產管道。在本地構建和測試(例如,您的開發盒託管您的服務器的本地版本),然後在準備黃金時間時推送到產品。您將能夠設計一個構建良好的系統,經受「使用」的考驗。即使你發現一個方法有一個服務器進程'本地'的PHP文件,不這樣做。您的服務器將在未來一段時間深入黑客攻擊。 – YvesLeBorg
欣賞評論先生。謝謝! –