0

我正在构建一个使用 jQuery/AJAX 将数据发送到 php 页面的网站,然后从那里将其插入数据库。出于某种原因,没有插入代码,我根本没有得到任何回应。

我的JavaScript:

    function insert_data(){
    var title = debate_title.value;
    var subtitle = debate_sub.value;
    var sides = debate_sides.value;

    $(function() {
        $.ajaxSetup({
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    window.location.replace('errors/noConnection.html');
                } else if (jqXHR.status == 404) {
                    window.location.replace('errors/noConnection.html');
                } else if (jqXHR.status == 500) {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'parsererror') {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'timeout') {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'abort') {
                    window.location.replace('errors/noConnection.html');
                } else {
                    window.location.replace('errors/noConnection.html');
                }
            }
        });
    });

    $.ajax({
        type: "POST",
        url: "post_debate.php",
        data: { post_title: title, post_sub: subtitle, post_sides: sidesm, ajax: 1 },
        dataType: "json",
        timeout: 5000, // in milliseconds
        success: function(data) {
            if(data!==null){
                window.location.replace('show_debate.php?id=' + data);
            }else{
                window.location.replace('errors/noConnection.html');
            }
        }
    });
}

我的 PHP 代码(post_debate.php):

    <?php

    require('connect.php');

    $title = $_POST['post_title'];

    $subtitle = $_POST['post_sub'];

    $sides = $_POST['post_sides'];

    $ajax = $_POST['ajax'];

    $date = new DateTime();
    $timeStamp = $date->getTimeStamp();

    if($ajax==1){
        $query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
        $get_data = mysql_query("SELECT id FROM debates WHERE title='$title', subtitle='$subtitle', sides='$sides', timestamp='$timeStamp'");
        while($id=mysql_fetch_array($get_data)){
            $final_id = $id['id'];
        }
        exit($final_id);
    }else{
        die("404 SERVER ERROR");
    }

?>

谢谢!


编辑 - 尚未解决

我的新 PHP 代码:

<?php
header("content-type: application/json");

require('connect.php');

$title = $_POST['post_title'];

$subtitle = $_POST['post_sub'];

$sides = $_POST['post_sides'];

$ajax = $_POST['ajax'];

$date = new DateTime();
$timeStamp = $date->getTimeStamp();

if($ajax==1){
    $query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
    $get_data = mysql_query("SELECT id FROM debates WHERE title='$title', subtitle='$subtitle', sides='$sides', timestamp='$timeStamp'");
    while($id=mysql_fetch_array($get_data)){
        $final_id = $id['id'];
    }
    print (json_encode(array("Id"=>$final_id)));
}else{
    die("404 SERVER ERROR");
}

?>

我的新 Javascript .ajax:

    $.ajax({
    type: "POST",
    url: "post_debate.php",
    data: { post_title: title, post_sub: subtitle, post_sides: sides, ajax: 1 },
    dataType: "json",
    timeout: 5000, // in milliseconds
    success: function(data) {
        if(data!==null){
            window.location.replace('show_debate.php?id=' + data['Id']);
        }else{
            window.location.replace('errors/noConnection.html');
        }
    }
});
4

1 回答 1

1

您的代码期望 JSON 作为响应...

dataType: "json",

文档在这里

但是您返回的是一个没有适当内容类型标头的非 json 值。

尝试更改您的 PHP 脚本

   exit($final_id);

到(未经测试)

header("content-type: application/json");
print (json_encode(array(
        "Id"=>$final_id
    )));

此外,在 Javascript 代码中的回调上放置一个断点success(使用 Firebug 或类似工具)并检查data包含的内容。它现在应该是一个关联数组,所以你可以做

window.location.replace('show_debate.php?id=' + data['Id']);

改进:

不要执行 SELECT 来获取最近插入的 Id,而是使用mysql_insert_id(). 像这样的东西...

$query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
$final_id = mysql_insert_id();
print (json_encode(array("Id"=>$final_id)));

此外,如果您在开发工具中看不到响应,另一种测试 PHP 返回的方法是直接浏览到该页面(您必须将所有内容更改$_POST$_REQUEST

于 2012-12-14T15:35:49.303 回答