首页 > 解决方案 > 为什么 JSON 响应没有得到响应?

问题描述

我将 PHP 用于我的 android 应用程序的服务器端。应用程序通过方法发送登录参数rollno密码POST。该代码正在获取结果并使用JSON. 我在实时服务器和本地主机上都托管了相同的系统。实时服务器工作正常,但本地服务器没有响应响应。代码如下:

// an array to display response
$response = array();
if (isset($_GET['apicall'])) {
    switch ($_GET['apicall']) {
    case 'login':
        // this part will handle the login
        // for login we need the email and password
        if (isTheseParametersAvailable(array(
            'rollno',
            'password'
        ))) {
            // getting server ip for building image url
            $server_ip = gethostbyname(gethostname());
            // image upload path
            define('UPLOAD_PATH', 'uploads/');
            // getting values
            $rollno = $_POST['rollno'];
            $user_password = $_POST['password'];
            $sql = "SELECT password from users WHERE rollno = \"$rollno\"";
            $result = $conn->query($sql);
            while ($row = $result->fetch_assoc()) {
                $pass_hash = $row["password"];
            }
            if (password_verify($user_password, $pass_hash)) {
                // creating the query
                $stmt = $conn->prepare("SELECT id, username, email, gender, rollno, image, personal_notice FROM users WHERE rollno = ?");
                $stmt->bind_param("s", $rollno);
                $stmt->execute();
                $stmt->store_result();
                // query for fetching fees records
                $feesqry = $conn->prepare("SELECT total, paid, remaining FROM fees WHERE rollno = ?");
                $feesqry->bind_param("s", $rollno);
                $feesqry->execute();
                $feesqry->store_result();
                // query for fetching attendance records
                $attqry = $conn->prepare("SELECT mon, tue, wed, thrs, fri, sat, sun FROM attendance WHERE rollno = ?");
                $attqry->bind_param("s", $rollno);
                $attqry->execute();
                $attqry->store_result();
                // query for fetching test records
                $tstqry = $conn->prepare("SELECT tst1_date, tst1_marks, tst1_rank, tst2_date, tst2_marks, tst2_rank, tst3_date, tst3_marks, tst3_rank, tst4_date, tst4_marks, tst4_rank, tst5_date, tst5_marks, tst5_rank FROM test WHERE rollno = ?");
                $tstqry->bind_param("s", $rollno);
                $tstqry->execute();
                $tstqry->store_result();
                // query for fetching common notice
                $noticeqry = $conn->prepare("SELECT common_notice FROM notice");
                $noticeqry->execute();
                $noticeqry->store_result();
                // if the user exist with given credentials
                if ($stmt->num_rows > 0) {
                    $stmt->bind_result($id, $username, $email, $gender, $rollno, $image_temp, $personal_notice);
                    $stmt->fetch();
                    $image = 'http://' . $server_ip . '/arsod_dynamic/' . UPLOAD_PATH . $image_temp;
                    $feesqry->bind_result($total, $paid, $remaining);
                    $feesqry->fetch();
                    $attqry->bind_result($mon, $tue, $wed, $thrs, $fri, $sat, $sun);
                    $attqry->fetch();
                    $tstqry->bind_result($tst1_date, $tst1_marks, $tst1_rank, $tst2_date, $tst2_marks, $tst2_rank, $tst3_date, $tst3_marks, $tst3_rank, $tst4_date, $tst4_marks, $tst4_rank, $tst5_date, $tst5_marks, $tst5_rank);
                    $tstqry->fetch();
                    $noticeqry->bind_result($common_notice);
                    $noticeqry->fetch();
                    $user = array(
                        'id' => $id,
                    // Various other key/values here
                    $response['user'] = $user;
                }
                else {
                    // if the user not found
                    $response['error'] = false;
                    $response['message'] = 'Invalid Roll No. or Password';
                }
            }
            else {
                // if the user not found
                $response['error'] = false;
                $response['message'] = 'Invalid Roll No. or Password';
            }
        }
        break;
        else {
            // if it is not api call
            // pushing appropriate values to response array
            $response['error'] = true;
            $response['message'] = 'Invalid API Call';
        }
        // displaying the response in json structure
        echo json_encode($response);
        // function validating all the paramters are available
        // we will pass the required parameters to this function
        function isTheseParametersAvailable($params)
        {
            // traversing through all the parameters
            foreach($params as $param) {
                // if the paramter is not available
                if (!isset($_POST[$param])) {
                    // return false
                    return false;
                }
            }
            // return true if every param is available
            return true;
        }

上面的代码有行echo json_encode($response);也,json不返回。请帮忙。

像这样在实时服务器上有成功的 JSON 输出

{"error":false,"message":"Login successfull","user":{"id":498,"username":"Test Entry","email":null,"gender":"NA","rollno":1000,"image":"https:\/\/softglobe.net\/client-projects\/arsodclasses\/dynamic\/uploads\/default-pic.png","personal_notice":"Not available yet","common_notice":"Freedom in Mind;Faith in Words;Pride in our Heart; Memories in our Souls.Let\u2019s salute the Nation on Republic Day.HAPPY REPUBLIC DAY!","total":"-","paid":"-","remaining":"-","mon":"-","tue":"-","wed":"-","thrs":"-","fri":"-","sat":"-","sun":"-","tst1_date":"-","tst1_marks":"-","tst1_rank":"-","tst2_date":"-","tst2_marks":"-","tst2_rank":"-","tst3_date":"-","tst3_marks":"-","tst3_rank":"-","tst4_date":"-","tst4_marks":"-","tst4_rank":"-","tst5_date":"-","tst5_marks":"-","tst5_rank":"-"}}

但是在本地服务器上,没有任何响应打印出来。

标签: phpjson

解决方案


您在第一个 switch 案例结束后回应了响应。在中断语句之前回显


推荐阅读