如何调试jQuery的AJAX调用?jQuery、AJAX

2023-09-10 14:12:56 作者:誓言开始慢慢沉淀つ

我一直在试图让AJAX来用jQuery工作。我的大问题,到目前为止一直是我真的不知道该怎么揣摩出我犯了一个错误。我真的没有AJAX调用调试的好方法。

I have been working on trying to get AJAX to work with Jquery. My big issue so far has been that I don't really know how to figure out where I'm making a mistake. I don't really have a good way to debug AJAX calls.

我试图建立一个管理页面,在这里我想作的功能之一是改变权限集在我的SQL数据库。我知道了。点击功能被触发,所以我已经缩小下来,但我不知道从AJAX调用SQL查询其走错了。

I'm trying to set up an admin page where one of the functions I want to make is to change the permission set in my SQL database. I know that the .click function is being triggered, so I've narrowed that down, but I'm not sure where in the chain from AJAX call to SQL query its going wrong.

我的.js code:

My .js code:

$('#ChangePermission').click(function(){
    $.ajax({
        url: 'change_permission.php',
        type: 'POST',
        data: {
        'user': document.GetElementById("user").value,
        'perm': document.GetElementById("perm").value
        }
    })
})

我的.php处理程序:

my .php handler:

<?php  
require_once(functions.php);

echo $_POST["user"];

try{
    $DBH = mysql_start();

    $STH = $DBH->prepare("INSERT INTO people ( username, permissions ) values (?, ?)");

    $STH->bindParam(1, $_POST["user"]);
    $STH->bindParam(2, $_POST["perm"]);

    $STH->execute();
}
catch(PDOException $e){
    echo $e->getMessage;
}?>

当mysql_start配置好后,我在我的其他SQL调用成功使用PDO功能。

Where the mysql_start is setup for the PDO function that I use successfully in my other SQL calls.

我一直在研究和查找教程了几天,现在我不能为我的生活找不出什么错误。是否有工具,我可以用它来找出其中的错误出现?我在回答这个具体问题显然是感兴趣,但我觉得我比较大的问题在这里,我不知道从哪里开始调试。感谢您的帮助!

I have been researching and looking up tutorials for a few days now and I can't for the life of me figure out what's going wrong. Are there tools I can use to figure out where the error is occuring? I'm obviously interested in the answer to this specific issue, but I think my bigger issue here is that I have no idea where to start with debugging. Thanks for your help!

推荐答案

请您JQuery的电话通过添加成功和错误回调更稳健。

Make your JQuery call more robust by adding success and error callbacks.

$('#ChangePermission').click(function(){
    $.ajax({
        url: 'change_permission.php',
        type: 'POST',
        data: {
        'user': document.GetElementById("user").value,
        'perm': document.GetElementById("perm").value
        },
       success:function(result)//we got the response
       {
        alert('Successfully called');
       },
       error:function(exception){alert('Exeption:'+exception);}


    })
})