Ajax错误函数未触发

时间:2014-10-10 20:53:34

标签: jquery ajax json

当有结果显示时,我的功能完美执行。但是,如果没有结果,或者在我的情况下,要显示程序主机记录,我希望显示“无结果”的消息。然而,没有任何反应。有任何想法吗?谢谢你的帮助!

$('#Lookup').click(function() {
var form_data = $('#validation-form').serializeArray();

$.ajax({
    url: "<?php echo site_url('opportunity/search_existing_program_host'); ?>",
    type: 'POST',
    data: form_data,
    cache: false,
    success: function(server_response) {
        var data = $.parseJSON(server_response);
        var html = '', program_host;

        html += '<tr><th>Program Host Name</th><th>Location</th><th>Session Prefix</th><th>Agency No.</th></tr>';
        for(var i = 0; i < data.length; i++){
            program_host = data[i];
            html += '<tr><td>' + program_host.Program_Host_Name + '</td><td>' + program_host.Address_1 + ', ' + program_host.City + ', ' + program_host.State + '</td><td>' + program_host.Session_Number_Prefix + '</td><td>' + program_host.Agency_Number + '</td></tr>';
        }

        $('#existing_program_hosts').append(html);
    },
    error: function() {
        //alert('Error');
        var html = '';

        html += '<tr><th>No Results</th></tr>';
        $('#existing_program_hosts').append(html);
    }
});

$('#Create_Host_btn').show();

})

这是我返回结果的函数:

function search_existing_program_host() {
    $Program_Host_Name = $this->input->post('Program_Host_Name');
    $Address_1 = $this->input->post('Address_1');

    $program_hosts = $this->ion_auth_model->search_existing_program_host($Program_Host_Name, $Address_1);

    foreach($program_hosts as $program_host):
        $results[] = array(
            "Program_Host_Name"         => "$program_host->Name",
            "Session_Number_Prefix"     => "$program_host->Session_Number_Prefix",
            "Agency_Number"             => "$program_host->Agency_Number",
            "Address_1"                 => "$program_host->Address_1",
            "Address_2"                 => "$program_host->Address_2",
            "Zip_Code"                  => "$program_host->Zip_Code",
            "Primary_Phone_Number"      => "$program_host->Primary_Phone_Number",
            "Secondary_Phone_Number"    => "$program_host->Secondary_Phone_Number",
            "Fax_Number"                => "$program_host->Fax_Number",
            "Number_501c3"              => "$program_host->Number_501c3",
            "Expiration_501c3"          => "$program_host->Expiration_501c3",
            "City"                      => "$program_host->City",
            "State"                     => "$program_host->State"
        );
    endforeach;

    echo json_encode($results);

}

2 个答案:

答案 0 :(得分:1)

就个人而言,我会做类似的事情:

$('#Lookup').click(function() {
var form_data = $('#validation-form').serializeArray();
var html = '<tr><th>No Results</th></tr>';

$.ajax({
    url: "<?php echo site_url('opportunity/search_existing_program_host'); ?>",
    type: 'POST',
    data: form_data,
    cache: false,
    success: function(server_response) {
        var data = $.parseJSON(server_response);
        var program_host;

        if (server_response != null){
            html += '<tr><th>Program Host Name</th><th>Location</th><th>Session Prefix</th><th>Agency No.</th></tr>';
            for(var i = 0; i < data.length; i++){
                program_host = data[i];
                html += '<tr><td>' + program_host.Program_Host_Name + '</td><td>' + program_host.Address_1 + ', ' + program_host.City + ', ' + program_host.State + '</td><td>' + program_host.Session_Number_Prefix + '</td><td>' + program_host.Agency_Number + '</td></tr>';
            }
         }

    },
    error: function(e, textStatus) {
        // perhaps you want something a little more custom here?
        html += '<tr><th>' + textStatus + '</th></tr>';
    },
    complete: function(){
       $('#existing_program_hosts').append(html);
    }
});

答案 1 :(得分:0)

成功&#34;成功&#34;功能,您可能需要检查&#34; server_response&#34;的值。看看它是否为空。使用check

将代码包含在success函数中
if (server_response != null) {
....current code...
} else {
...do soemthing if no data is returned.
}