jQuery AJAX成功回调不工作的JSON数据类型


jQuery AJAX Success Callback is not Working for JSON data type

我试图从ajax_autocomplete.php获得一些JSON数据,然后在成功回调中做一些事情。问题是我不能进入成功回调,错误回调也没有说什么。

我检查了我的JSON,它是有效的,我已经尝试了两个小时了,找不到解决方案。

$("#reg").autocomplete({
  minLength: 2,
  dataType: 'json',
  source: 'ajax_autocomplete.php',
  success : function(data){
    alert('success');
  },
  error : function(xhr, status){
    console.log(status);
  }

});这是我的JSON:

[{"label":"Test id 1","id":"1"},{"label":"Test id 2","id":"2"}]

我试着添加

type: 'GET',
async : false,

但是它不起作用。

$( "#reg" ).autocomplete({
    minLength: 2,
    source: function( request, response ) {
        $.ajax({
            url: "ajax_autocomplete.php",
            dataType: "json",
            success: function( data ) {
                response( $.map( data, function( item ) {
                  return {
                    label: item.label,
                    value: item.id
                  }
                }));
                // do something
            }
        });
    },
    select: function( event, ui ) {
        // ...
    },
    open: function() {
        // ...
    },
    close: function() {
        // ...
    }
});
  $.ajax({
                url : "ajax_autocomplete.php",
                data : {},
                type : "GET",
                success : function(data) {
                          alert(data);
                },
                error : function(err) {
                             alert(err);
                }                       
    });