javascript - Cordova - http 请求不处理本地 php

标签 javascript php ajax xml

这是我的第一个 Cordova 项目。我的http请求不使用POST处理本地php文件。当 php 文件托管在服务器上时,代码可以正常工作并处理它。但当 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");
    }
});
});

alert(data) 显示整个 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";
}
?>

最佳答案

您无法在 cordova 项目上运行 PHP 代码。 PHP 需要在支持 PHP 的环境(例如您的服务器)上执行。你的php代码也使用了一个数据库,这个数据库需要在服务器上配置,这可以在本地完成,但显然不能在设备之间共享。

看来你不太清楚 PHP 是什么。您应该开始学习它并了解它是如何工作的。

关于javascript - Cordova - http 请求不处理本地 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807423/

相关文章:

javascript - javascript在grails 3.1.9中不会显示

javascript - React Native - 如何将组件用作 JavaScript 对象?

javascript - 如何从控制台动态设置变量?

混帐/ Mercurial : Separate repos in structure

php - 取消设置在 php 中不起作用的错误消息

javascript - Ajax 请求返回 200 OK,但触发了错误事件而不是成功

javascript - Material Angular 表多行指令

Javascript/Jquery .html() 仅替换一次

php - 我应该如何完成我的评级脚本

php - 当我们有各自地点的当前时间和时区时,两个城市/国家之间的时差