七度&光 2017-02-07 18:16 采纳率: 22.2%
浏览 20

AJAX没有收到错误

I'm completely new to JSON, JS and AJAX. I'm used some example codes to progress through my targets. What I don't understand is when the JSON is an error object the success section is still firing. The console shows error where object d is null or a non-object when error is true. What have I done incorrectly? TIA

Result type good:

JSON: {"data":["1","breast","2","wing","3","thigh","4","leg","5","half","6","quarter white","7","quarter dark","9","whole"],"error":false}

Result type error:

JSON: {"error":true}

$.ajax({
            type     : 'GET',
            url      : 'getsubtypes2.php',
            data     : dataString,
            dataType : 'JSON',
            cache: false,
            success  : function(data) {          
                var output;            
                var d = data.data;
                    var output = "";
                    for (var i = 0 ; i< d.length; i=i+2) {//error gets to this line
                      var count = d[i];
                      var newOption = d[i+1];
                      output += "<option value='"+count+"'>"+newOption+"</option>";
                    }
                    $('#select3').empty().append(output); 
            },
            error: function(){
            $('#select3').empty();
                    console.log("Ajax failed");
            }
        });
  • 写回答

3条回答 默认 最新

  • python小菜 2017-02-07 18:20
    关注

    The server must return an error status code such as 4xx or 5xx for the error callback to execute. If you server is returning a 2xx, the success callback will execute.

    For testing you can try explicitly sending an error code to see the error callback executing. See this question.

    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析