doulangpeng3933 2019-01-25 10:53
浏览 265

为什么JSON响应没有得到回应呢?

I am using PHP for the server side of my android app. The app sends the login parameters rollno and password with POST method. The code is fetching the result and echoing the response with JSON. I have hosted the same system on the live server as well as on my localhost. The live server works fine, but the local server is not echoing the response. The code is as below:

// 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;
        }

The above code has line echo json_encode($response); the also, json is not returned. Please help.

There is successful JSON output on the live server like this

{"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":"-"}}

But on the local server, nothing prints as a response.

  • 写回答

1条回答 默认 最新

  • duanche5149 2019-01-25 11:10
    关注

    you echoed response after the end of first switch case. echo before break statement

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法