解析JSON请求失败 - FIXED

时间:2014-07-09 15:42:34

标签: php jquery mysql ajax json

一切正常,数据已经保存到mySQL中但问题是我仍然遇到此错误"解析JSON请求失败"。我不太确定错误的来源及其原因。

脚本:

$("#add_submit_form").on("submit", function(event) {
  event.preventDefault();
  console.log($(this).serialize());

  $.ajax({
    type: "POST",
    url: "add_submit.php",
    data: "subject_code="+subj_code,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data){
      alert("success");
    },
    error:function(x,e){
        if(x.status==0){
            alert('You are offline!!\n Please Check Your Network.');
        }else if(x.status==404){
            alert('Requested URL not found.');
        }else if(x.status==500){
            alert('Internel Server Error.');
        }else if(e=='parsererror'){
            alert('Error.\nParsing JSON Request failed.');
        }else if(e=='timeout'){
            alert('Request Time out.');
        }else {
            alert('Unknow Error.\n'+x.responseText);
        }
    }


  });        
});

PHP:

header('Content-type: application/json');
require_once('/include/connect.php');

/** Pass the user inputs into re-useable variables **/
$subject_code = mysql_real_escape_string(trim($_POST['subject_code']));
$subject_name = mysql_real_escape_string(trim($_POST['subject_name']));
$subject_course = mysql_real_escape_string(trim($_POST['subject_course']));
$subject_day = mysql_real_escape_string(trim($_POST['subject_day']));
$subject_time = mysql_real_escape_string(trim($_POST['subject_time']));
$subject_year = mysql_real_escape_string(trim($_POST['subject_year']));
$subject_section = mysql_real_escape_string(trim($_POST['subject_section']));
$subject_room = mysql_real_escape_string(trim($_POST['subject_room']));

/** Prepare the SQL statement **/
$stmt = $database->prepare('
    INSERT INTO schedule(
        sched_id,
        subject_code,
        subject_name,
        subject_course,
        subject_day,
        subject_time,
        subject_year,
        subject_section,
        subject_room)
    VALUES(?,?,?,?,?,?,?,?,?)');

/** Bind the statement into parameters**/
$stmt->bind_param('issssssss',
        $sched_id,
        $subject_code,
        $subject_name,
        $subject_course,
        $subject_day,
        $subject_time,
        $subject_year,
        $subject_section,
        $subject_room);

/** Execute the statement **/
$stmt->execute();

if($stmt){
    $response_array['status'] = 'success';
}
else{
    $response_array['status'] = 'error';
}

echo json_encode($return);

exit;

HTML:

<form action="add_submit.php" method="POST" id="add_submit_form">
    <section id="add_subject_response">response</section> <!--Hidden section for RESPONSE on ADD SCHEDULE-->         
    <td>
        <select id="add_subject_code" name="subject_code">;
        <?php
            $stmt = $database->prepare('SELECT * FROM subject_schedule');
            $stmt->execute();
            $result = $stmt->get_result();
            echo "<option id='' value=''>Select Subject Code</option>";
                while ($row = $result->fetch_assoc()) {
                    $subj_code = $row['subject_code'];
                    $subj_name = $row['subject_name'];
                    $subj_course = $row['subject_course'];
                    echo "<option id='" . $subj_course . "' class='" . $subj_name  . "' value='" . $subj_code . "'>" . $subj_code . "</option>";
                }
        ?>
        </select>
    </td>
    <td><input id="add_subject_name" class="add_required" type="text" autocomplete="off" name="subject_name"></td>
    <td><input id="add_subject_course" class="add_required" type="text" autocomplete="off" name="subject_course"></td>
    <td><input id="add_subject_day" class="add_required" type="text" autocomplete="off" name="subject_day" maxlength="10"></td>
    <td><input id="add_subject_time" class="add_required" type="text" autocomplete="off" name="subject_time" maxlength="20"></td>
    <td><input id="add_subject_year" class="add_required" type="text" autocomplete="off" name="subject_year" maxlength="10"></td>
    <td><input id="add_subject_section" class="add_required" type="text" autocomplete="off" name="subject_section" maxlength="10"></td>
    <td><input id="add_subject_room" class="add_required" type="text" autocomplete="off" name="subject_room" maxlength="10"></td>
    <td><input id="add_submit_button" type="submit" value="Add Schedule"></td>
</form>

控制台日志:

subject_code=FIL111&subject_name=Sining+ng+Komunikasyon&subject_course=Bachelor+of+Science+in+Information+Technology&subject_day=day_test&subject_time=time_test&subject_year=year_test&subject_section=section_te&subject_room=room_test

更改:删除mysql_real_escape_string

脚本:

data: $(this).serialize(),

PHP:

echo json_encode($response_array);

结果:保存在mySQL上,控制台日志仍然相同,错误仍然相同

开发工具结果:

  

数组(8){
    [&#34; subject_code&#34;] =&GT;
    string(6)&#34; ENG111&#34;
    [&#34; SUBJECT_NAME&#34;] =&GT;
    string(20)&#34; Communication Arts 1&#34;
    [&#34; subject_course&#34;] =&GT;
    string(45)&#34;信息技术理学士&#34;
    [&#34; subject_day&#34;] =&GT;
    string(4)&#34; test&#34;
    [&#34; subject_time&#34;] =&GT;
    string(4)&#34; test&#34;
    [&#34; subject_year&#34;] =&GT;
    string(4)&#34; test&#34;
    [&#34; subject_section&#34;] =&GT;
    string(4)&#34; test&#34;
    [&#34; subject_room&#34;] =&GT;
    string(4)&#34; test&#34;
  }   {&#34;状态&#34;:&#34;成功&#34;}

FIX:添加了一些代码

PHP:

$response_array = array('subject_code' => $subject_code,
                        'subject_name' => $subject_name,
                        'subject_course' => $subject_course,
                        'subject_day' => $subject_day,
                        'subject_time' => $subject_time,
                        'subject_year' => $subject_year,
                        'subject_section' => $subject_section,
                        'subject_room' => $subject_room);

将if / else语句更改为(PHP):

if($stmt->affected_rows > 0){
    echo json_encode($response_array);
}
else{
    $response_array['status'] = 'error';
}

0 个答案:

没有答案