如何使用 jQuery 测试 AJAX 帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5751399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:45:55  来源:igfitidea点击:

How to test AJAX post with jQuery

jqueryajax

提问by santa

I have the following function that opens jQuery UI's dialog warning about the delete of an entry. Everything works fine, except the POST with AJAX. I get response on "success" but I don't believe I am hitting the .php page that is supposed to execute the query. Am I missing anything here?

我有以下函数打开 jQuery UI 的关于删除条目的对话框警告。一切正常,除了带有 AJAX 的 POST。我得到了“成功”的回应,但我不相信我正在点击应该执行查询的 .php 页面。我在这里错过了什么吗?

var $dialog = $("#dialog").dialog({ autoOpen: false, resizable: false, modal: true });

    $(".delProj").click(function(){ 
        var delURL = $(this).attr("href").split("#");
        var projID = delURL[1];
        $dialog.dialog("open");
        $("#dialog").dialog({
            buttons: {
                "Yes, delete this test": function() {
                    $.ajax({
                        type: "POST",
                        url: "http://www.example.com/inc/db_actions.php",
                        data: "op=DeleteProject&delete="+projID,
                        success: function(){
                          //  alert( "Test Project deleted successfully." );
                          $("#"+projID).remove();
                        }
                    });
                    $(this).dialog("close");
                },
                "No, keep it.": function() {
                    $(this).dialog("close");
                }
            }
        });
    });

回答by Marc Bouvier

Make your php page called echo something

使您的 php 页面称为 echo something

PHP

PHP

echo 'hello world';

Javascript

Javascript

Add the return value of the callback (here data), and try to show it with js.

添加回调的返回值(这里data),并尝试用js展示。

[..]

[..]

success: function(data){
    alert(data);
}

[...]

[...]

回答by Demian Brecht

I'll usually send some form of a response through JSON-formatted data so that my AJAX queries know whether or not they've accomplished what they've set out to do. Something like:

我通常会通过 JSON 格式的数据发送某种形式的响应,以便我的 AJAX 查询知道他们是否已经完成了他们打算做的事情。就像是:

Script:

脚本:

$.ajax({
    type: "POST",
    url: "http://www.mydomain.com/inc/db_actions.php",
    data: "op=DeleteProject&delete="+projID,
    success: function(data){
        if(data.success == true)
        {
            $("#"+projID).remove();
        }
});

PHP:

PHP:

// do something
echo json_encode(array('success'=>true));

Edit:

编辑:

It's also usually a good thing to trap your ajax errors:

捕获 ajax 错误通常也是一件好事:

$.ajax({
    type: "POST",
    url: "http://www.mydomain.com/inc/db_actions.php",
    data: "op=DeleteProject&delete="+projID,
    success: function(data){
        if(data.success == true)
        {
            $("#"+projID).remove();
        }
    },
    error: function(){
        alert('something bad happened');
    }
});