如何从AJAX请求获取有关错误的详细信息

时间:2013-06-03 15:51:38

标签: jquery xml ajax

我有一个向XML页面发出请求的函数:

$.ajax({
    type: "GET",
    url: "results.xml",
    error:  function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    },
    dataType: "xml",
    success: xmlParser
});

请求失败,并显示错误消息“error”。如何获得有关失败原因的更多详细信息?

2 个答案:

答案 0 :(得分:0)

你是否尝试打开像firebug控制台或其他一些工作人员那样的东西。你可以看到有什么问题,因为你有console.log(textStatus,errorThrown);这将写在我提到的控制台中。

答案 1 :(得分:0)

简单地说。在firebug网络标签中,如果您没有看到您的被叫网址出现添加"返回false;"阻止您的表单正常提交(因为您将使用Ajax执行此操作)。

<form name="myForm" id="myForm" method="post" action="">
<input type="text" name="fieldName" id="fieldName" value="" />
<input value="Save" id="saveButton" type="submit">
<br /><span class="show_error"></span>
</form>

$(document).ready(function(){ 
    $("#myForm").submit(function(){
    processForm();  // do Ajax stuff
    **return false;**   // stop normal form submission so you can do it with Ajax. 
    });

   function processForm(){ 
     var fieldName = $('input[name="fieldName"]').val();
     var request = $.ajax({
     type: "POST",
     url: "ajaxhandler.php",
     data: { fieldName: fieldName},
     dataType: "json" //Type of response to expect from server
     });
     request.done(function (msg){
     console.log("Worked");
     });
     request.fail(function (textStatus){
    $('.show_error').fadeIn(1000).html(textStatus); 
    console.error("Failed: "+ textStatus);
    });
    }

});

然后查看ajax提交的url的响应选项卡(在我的情况下是一个php文件),并确保它返回一些东西(在我的情况下是json编码的成功或失败消息)。如果它没有返回任何内容,那么您只会出现错误,指出错误&#39;。

为什么没有返回任何内容,为什么我无法获得任何错误详细信息?事实证明,当我使用端口443进行我的ajax调用时,我从不同于我自己的服务器加载我的jQuery而没有指定https。

更改

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> was the fix for me. 
相关问题