angularjs中的错误请求错误

时间:2018-03-02 08:41:13

标签: javascript php angularjs json http-post

var successCallback=function(response) {
    if(response.success) {
      $log.log(response.data);
      alert('fetched courses and percentages successfully');
    } else {
    }
};
var errorCallback = function(response) {
  console.log(response.success);
  alert( "failure message: " + JSON.stringify(response));
};
var data = { "mis": 111608059};
// data = JSON.stringify(data),
$http.post('api/stu_course_%.php', data).then(successCallback, errorCallback);     

上面的代码给出了以下错误:

  

失败消息:{“data”:“\ n \ n400错误请求\ n \ n

错误请求

您的浏览器发送了此服务器无法理解的请求。


Apache / 2.4.18(Ubuntu)服务器在localhost端口80 \ n \ n“,”status“:400,”config“:{”method“:”POST“, “transformRequest”:[空], “transformResponse”:[空], “jsonpCallbackParam”: “回调”, “URL”: “API / stu_course _%的php”, “数据”:{ “MIS”:111608059},” headers“:{”Accept“:”application / json,text / plain, / “,”Content-Type“:”application / json; charset = utf-8“}},”statusText“ :“错误请求”,“xhrStatus”:“完成”}

php中的服务器端代码:

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    require 'config.php';

    function array_push_assoc($array, $key, $value){
        $array[$key] = $value;
        return $array;
    }
    $json_request = file_get_contents('php://input');
    $request = json_decode($json_request, true);
    print_r( $request);
    $MIS = $request["mis"];
    //$MIS = 111608059;
    $data = array();
    $result1 = mysqli_query($conn, "select enrolled.course_id, course_code, course_name from enrolled LEFT OUTER JOIN courses ON enrolled.course_id=courses.course_id where MIS='$MIS'");
    if (mysqli_num_rows($result1) > 0) {
        while($row = mysqli_fetch_assoc($result1)){
            $query2 = "select count(course_id) as count from attendance_item group by course_id, MIS having (MIS='$MIS' and course_id='$row[course_id]') ";
            $query3 = "select count(course_id) as total_count from lecture group by course_id having course_id='$row[course_id]'";
            $result2 = mysqli_query($conn, $query2);
            $result3 = mysqli_query($conn, $query3);

            $count = mysqli_fetch_assoc($result2);
            // print_r ($count);
            $total_count = mysqli_fetch_assoc($result3);
            // print_r ($total_count);
            $percent = $count["count"]/$total_count["total_count"] *100;
            // echo $percent;
            $data = array_push_assoc($data, $row["course_name"], $percent);
        }
        // print_r($data);
        $success = 1;
        json_encode($data);
    }
    else{
        $success = 0;
    }
    $response = array();
    $response["success"] = $success;
    $response["data"] = $data;
    echo json_encode($response);
?>

0 个答案:

没有答案