Jquery Ajax自动完成,在ajax成功时调用函数并在Json对象数组上调用自动完成

时间:2016-07-23 09:39:35

标签: javascript jquery ajax

我从document.ready调用一个函数来调用ajax。如果ajax调用成功收到响应,则在其中调用回调函数。在回调函数中,我正在json对象数组上编写自动完成。但是自动完成事件由于某种原因没有触发。

  $(document).ready(function(){
        getTheSearchDataForAutoComplete();
    });


function getTheSearchDataForAutoComplete(){



$.ajax({
        type: "POST",

        url: 'url',


        contentType: "application/json",
        data: formToJSONGetDoctorIdAndRole(),
        success: function(response, status, jqXHR){ 

                //The callback function in which using the autocomplet
                callback(response.payload)



        },
        error: function(err){
            alert('ERROR IN getTheSearchDataForAutoComplete ' + err.status);

        }
    });

}

下面给出的是回调函数,但在回调函数中,autocomplete事件没有被触发。

 function callback(result){

    //var prescriptionDataForSearch = result;


 //It is the raw to test
 var data = [
    {
        "id": 1,
        "first_name": "Will",
        "last_name": "Smith",
        "created_at": "2015-01-27T13:09:20.243Z",
        "updated_at": "2015-01-27T13:09:20.243Z"
    },
    {
        "id": 2,
        "first_name": "Willem",
        "last_name": "Dafoe",
        "created_at": "2015-01-27T13:17:23.479Z",
        "updated_at": "2015-01-27T13:17:23.479Z"
    }
];

     $('#add-symptom-textbox').autocomplete({

        minLength: 2,
        source: function (request, response) {


           var array = $.map (data, function (value, key) {

                return {
                    label: value.first_name,
                    //value: value.first_name
                }
            });

            //Filter the data locally
             response($.ui.autocomplete.filter(array, request.term));

        }

    });  

}

但是当我从document.ready中调用该函数时。它正在发挥作用。

   $(document).ready(function(){

       callback();
    });

问题出在哪里?

0 个答案:

没有答案