當有結果顯示時,我的函數可以很好地執行。但是,如果沒有結果,或者在我的情況下,顯示程序主機記錄,我希望顯示「無結果」消息。然而,沒有任何反應。有任何想法嗎?謝謝您的幫助!Ajax錯誤功能未觸發
$('#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);
}
不過不是爲我工作。在我看來,如果結果爲空,成功不會執行。但是,錯誤也沒有做到。 – 2014-10-13 13:30:38