PHP调用的JQuery Ajax内部服务器错误

时间:2015-10-29 13:53:35

标签: php jquery ajax

我对这些东西很陌生,到目前为止,当我只传递1 $_POST变量时,我可以使它工作。

但是在这种情况下,我试图传递2个变量,由于某种原因,我得到Internal Server Error作为结果。现在我做了一些跟踪,它肯定不是SQL错误。我确定错误源于Ajax部分,我正在通过POST,但我无法弄清楚我做错了什么。任何人都可以帮忙吗?

这是我的Ajax代码:

$('.class_checkbox').on('click',function(){
    var split = this.id.split('_');
    var catID = split[0];
    var myData = 'catToInsert='+ $id; //build a post data structure
    var categoryId = 'category='+catID;

    jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "includes/category.php", //Where to make Ajax calls
        dataType:"html", // Data type, HTML, json etc.
        data:{myData: myData, categoryId: categoryId}, //Form variables
        success:function(response){
             alert(response);
        },
        error:function (xhr, ajaxOptions, thrownError){
            //On error, we alert user
            alert(thrownError);
        }
    });
    $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'));

});       

这是我的PHP:

<?php 
    include_once('config.php');
    $id = $_POST['catToInsert'];
    $catID = $_POST['category'];


    //Create PDO Object
    $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    //Set Error Handling for PDO
    $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    //Query

    $sql = "SELECT category_id from product_category WHERE product_id=$id";

    //Prepare Statement
    $stmt = $con->prepare($sql);
    $stmt->execute();

    $result = $stmt->fetchAll();
    $array = array_map('current', $result);

    echo $id;
?>

1 个答案:

答案 0 :(得分:2)

正如@kingkero所说,你将获得myDatacategoryId作为$ _POST中的密钥。 jquery将对你的POST做什么是这样的:

$_POST[categoryId]= "categoryId=someIDofYours"

因此,您需要删除var categoryId中的串联并直接在数据中传递catID

data:{'categoryId':catID}