du512926 2016-11-07 22:44
浏览 39

Post方法不向php页面发送数据

I used post method to send data (username and password ) from xdk app to php file by XMLHttpRequest object,but i faced a problem. all data i sent by post method undefined in php file.

this xdk code

<!DOCTYPE html>
<html lang="en-US">
<head>
    <script type="text/javascript">
        function login() {
            var xhr = new XMLHttpRequest();
            var username1 = document.getElementById("user_name").value;
            var pass = document.getElementById("pass_word").value;
            var params = "username=" + username1 + "&password=" + pass;
            alert(params);
            xhr.open("post", "http://localhost/hospital/test.php", false);
            xhr.onload = function () {
                if (xhr.status == 200) {
                    var json_string = xhr.responseText;
                    test.innerHTML = json_string;
                }
                else if (xhr.status == 404) {
                    alert("Web Service Doesn't Exist", "Error");
                }
                else {
                    alert("Unknown error occured while connecting to server", "Error");
                }
            }
            xhr.send(params);
        }
    </script>
</head>
<body>
<div class="container">
    <div id="login">
        <img src="img/logo.png" width="100 px" height="100 px"/>
        <form onsubmit="login(); return false;">
            <fieldset class="clearfix">
                <p><span class="fontawesome-user"></span><input type="text" id="user_name"/></p>
                <p><span class="fontawesome-lock"></span><input type="password" id="pass_word"/></p>
                <p><input type="submit" value="تسجيل الدخول"/></p>
            </fieldset>
        </form>
        <div id="test"></div>
</body>
</html>

this is php code

<?php
$con = mysqli_connect("localhost", "root", "", "interactive");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST["username"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
}

$sql = "SELECT * FROM Caregiver WHERE UserName = $username && password =     $password ";
$result = mysqli_query($con, $sql);
$num = $result->num_rows;
if ($num) {
    $row = $result->fetch_array(MYSQLI_BOTH);
    $data = array('status' => 'success', 'username' => $row['UserName'], 'password' => $row['password']);
} else {
    $data = array('status' => 'failure', 'Error: ' . mysqli_error($con));
}
echo json_encode($data);
mysqli_close($con);
?>
  • 写回答

1条回答 默认 最新

  • dongxue163536 2016-11-09 12:57
    关注

    this is a little outside my wheelhouse, but i think you probably need a port here:

    xhr.open("post", "http://localhost/hospital/test.php", false);
    

    like

    xhr.open("post", "http://localhost:port/hospital/test.php", false);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看