You must change the submit button to the type button. Inputs of type submit automatically send the page, and in your case this is not necessary.
<input type="button" id="search-button">
Otherwise, you can prevent the default button action by using event.preventDefault () .
$("#search-button").click(function(event){ event.preventDefault(); $.ajax({ url: 'foo.php', data: mydata, type: 'get', dataType: 'json', contentType: 'application/json', success: function(data){ $("#content").html(data); } }); });
Since you are expecting html to return from the server, it is best to specify the dataType
that you expect is actually html. Note that json used to have dataType
. You can also specify the type of data that you pass to the server using contentType
, which is json in your case, so you should use application/json
.
In your comments, your dataType should be json.
I just noticed that your $(document).ready(function() {});
seems to be $(document).ready(function() {});
seems closed. You seemed to forget );
. Is this a problem with the copy?
After your last code edit, you seem to have no comma between your success and error.
$.ajax({ url: 'foo.php', data: mydata, type: 'get', dataType: 'json', success: function(data){ $("#content").html(data); },
source share