添加AJAX错误:&成功:打破常规

Adding AJAX error: & success: breaks script

本文关键字:成功 打破常规 添加 错误 AJAX      更新时间:2023-09-26

我有一个AJAX调用,使用以下代码可以正常工作:

            $.ajax({
               url: "filename.php",
               type:"POST",
               data: {
                   "pType":pType},
               dataType: "json", 
               complete:function(){
               $('body, html').animate({scrollTop:$('#adminFunctions').offset().top}, 'slow');
                    }
               });// end ajax call

但是当我尝试添加error: &成功参数的AJAX调用,脚本不再运行。

$.ajax({
url: "filename.php",
type:"POST",
data: {
      "pType":pType},
dataType: "json", 
success: function() {
    $('#resultDiv').html('Success!' + response.responseText);
    $('body, html').animate({scrollTop:$('#logoText').offset().top}, 'slow');
    $('#addRentalPropertyForm').slideUp();
    $('#resultDiv').slideDown();
    $('#adminFunctionsA').slideDown();
    },
error: function() {
    $('#resultDiv').html('A problem has occurred.' + response.responseText);
    $('body, html').animate({scrollTop:$('#logoText').offset().top}, 'slow');
    $('#resultDiv').slideDown();
    },
});// end ajax call

有谁能帮我指路吗?我似乎不明白为什么这是失败的。complete:标记工作正常,数据成功提交,但是当我添加错误和成功代码时,它什么也不做。任何帮助都将非常感激,我确信这是我缺少的一些基本的东西。

 $.ajax({
  url: "filename.php",
  type:"POST",
  data: {
          "pType":pType},
  dataType: "json", 
  contentType: "application/json; charset=utf-8",
  success:function(data) {
                    // This outputs the result of the ajax request
                        console.log(data);
                        alert ('Appears successful');
                        // $('#resultDiv').html('Success!' + response.responseText);
                        $('body, html').animate({scrollTop:$('#logoText').offset().top}, 'slow');
                        $('#addRentalPropertyForm').slideUp();
                        $('#resultDiv').slideDown();
                        $('#adminFunctionsA').slideDown();
                    },
                    error: function(errorThrown){
                        console.log(errorThrown);
                        alert ('Seems to be a problem');
                        // $('#resultDiv').html('A problem has occurred.' + response.responseText);
                        $('body, html').animate({scrollTop:$('#logoText').offset().top}, 'slow');
                        $('#resultDiv').slideDown();
                        return false;
                    }  
    });
  1. 您没有将response传递给errorsuccess回调:
  2. 因为你的dataTypejson response将不会有responseText属性
  3. 考虑将回调简化为仅console.log( response );一旦你知道了response的结构,你就可以编写任何你需要的逻辑。

    success: function(response) { $('#resultDiv').html('Success!' + response.responseText);//????

    error: function(response) { $('#resultDiv').html('A problem has occurred.' + response.responseText);//??

  1. 你不应该使用dataType:'json',除非你确定filename.php返回json。否则,您将得到一个 parseerror 。很可能这就是你得到的……返回的数据不能解析为json对象,因此会触发错误回调。使用dataType:'text'dataType:'html',你的成功处理程序应该触发

以下是无效的JSON:

<!-- DATA FROM ADD RENTAL HERE --> ↵↵
<script type="text/javascript">alert('Property ID: 34');
</script>
<script type="text/javascript">alert('Amenities ID: 34');</script>↵↵              ↵             <!-- END DATA FROM ADD RENTAL -->↵             ↵             ↵             ↵             <!-- START DATA FOR IMAGE UPLOAD -->↵             ↵             
<!-- END DATA FOR IMAGE UPLOAD -->↵                    ↵                
</div>↵