1

我对以下代码有疑问。

var sendJson = (JSON.stringify(comanda));

$.ajax({
    url: 'sendMail.php',
    type : "post",
    data: sendJson,
    success: function(data){
        alert("Comanda dumneavoastra a fost trimisa");
    }
}); 

似乎没有发送数据....知道为什么吗?

好的...我知道没有发送任何内容,因为我使用 firebug 监控请求。我没有收到任何错误,控制台中没有任何内容。检查它是否被激活,它是。

4

1 回答 1

7

这就是我的评论的意思:

var sendJson = (JSON.stringify(comanda));

$.ajax({
    url: '/resource_url_goes_here',
    type : 'POST',
    data: sendJson,
    success: function(data){
        /* implementation goes here */ 
    },
    error: function(jqXHR, textStatus, errorThrown) { 
        /* implementation goes here */ 
    }
}); 

请注意,ajax 请求error现在有一个回调。所有请求都应该有一个错误回调,以便您可以轻松识别何时发生错误(如您所见,firebug 不会捕获所有内容)。

有时我觉得有帮助的另一件事是StatusCodes

$.ajax({
    url: '/resource_url_goes_here',
    type : 'POST',
    data: sendJson,
    statusCode: {  
        404: function() {  
            /*implementation for HTTP Status 404 (Not Found) goes here*/
        },
        401: function() {  
            /*implementation for HTTP Status 401 (Unauthorized) goes here*/
        } 
    },
    success: function(data){
        /* implementation goes here */ 
    },
    error: function(jqXHR, textStatus, errorThrown) { 
        /* implementation goes here */ 
    }
});

这将在服务器返回特定状态代码(此代码段中的 404 和 401)时执行一个函数,并且您可以为所需的状态代码提供特定的处理程序。您可以在此处找到更多相关信息。

于 2012-06-22T17:14:19.393 回答