Ajax result from Codigniter is the whole HTML page instead of just the data
When I do console.log(data);
to get the Ajax result data, I get the whole page source instead of just the results, although the results appear inside the search form div (only in the returned data)
How do I get only the data?
This is the view:
<div id="form-all">
<form name="search" action="html_form_action.asp" method="post">
<input type="text" name="search" />
<input type="submit" value="חפש" />
</form>
</div&g开发者_C百科t;
<script>
$("form[0] :submit").live("click", function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "<?= site_url('pages/search') ?>",
data: {company : $("form[0] :text").val()},
success: function(data) {
console.log(data);
}
});
});
</script>
And this is the controller:
function search1()
{
$data['page_title'] = 'Search';
$this->load->view('head', $data);
$this->load->view('pages/search', $data);
$this->load->view('footer');
return json_encode($this->Company->get_companies_by_name($this->input->post('company')));
}
Yes. That is correct.
The data property of a jQuery AJAX request has nothing to do with the return value -- that is solely the responsibility of whatever the server sends combined with whatever happens in the success method. You will need to do some refactoring.
If it were me, I would consider this:
function ajax_search()
{
echo json_encode(
$this->Company->get_companies_by_name(
$this->input->post('company')
)->result());
}
And then in jQuery, simply change it to:
event.preventDefault();
$.ajax({
type: "POST",
url: "<?= site_url('pages/ajax_search') ?>",
data: {company : $("form[0] :text").val()},
success: function(data) {
console.log(data);
}
});
});
Try this:
data: {"company" : $("form[0] :text").val()},
精彩评论