如何从另一个ajax调用的错误函数中运行ajax调用?

时间:2013-07-13 06:43:49

标签: javascript jquery ajax

我正在尝试运行ajax调用。如果返回错误,我想捕获responseText并将其发送到另一个ajax调用,该调用有一个php脚本,将错误邮寄给我。

      $.ajax ({
            type: "POST",
            url: "includes/add_remove_favorite.php",
            dataType: "json",
            data: {
                "listingType" : listingType,
                "listingId" : listingId
            },
            success: function(data) {
                alert('The first ajax call worked');
            }, //end first ajax success
            error: function(data){ //If the listing doesn't update, display an error message
                var currentURL = window.location;       

                $.ajax ({
                    type: "POST",
                    url: "includes/error_email.php",
                    dataType: "json",
                    data: {
                        "url" : currentURL,
                        "errorMessage" : data.responseText
                    },
                    success: function(data) {
                        alert('The email sent successfully');
                    },

                    error: function(data){ //If the listing doesn't update, display an error message
                        alert('The email did not send');
                    }
                }); //end second ajax call

            } //end first ajax error
        }); //end first ajax call

我能够从第一个ajax调用中得到错误,所以我知道一个工作正常。但由于某种原因,第二个ajax调用上的邮件脚本将不会发送,并且它不会返回成功或错误消息,这告诉我ajax调用不起作用。

如何从另一个ajax调用的错误函数中运行ajax调用?

1 个答案:

答案 0 :(得分:1)

您需要使用window.location.hrefwindow.location是一个对象。