After trying some solutions from this and many other questions I wasn't able to get what is exact problem in my code. My code
$(document).ready(function() {
$("#botname").blur(function() {
$.ajax({
type: "POST",
url: "/tukaiexotic/rq/requisition/typhead",
contentType: "application/json; charset=utf-8",
success: function(result) {
$("#commmonname").val(result);
}
});
});
});
It returns my expected result, but with result, it returns the HTML code of the whole page.
What is wrong in code?
Server side script
function typhead_mod()
{
$this->db->select("fa_id,fa_code,fa_name");
$aaa=$this->db->get('tukai_farms')->result();
echo strip_tags($aaa);
}
If you still need to return json & html, you have your interface returning data in both format based on http Accept header value set by the client when making ajax call. Then your code for displaying html doesn't need to know about the returned data format because it only gets what it needs, which html.
While this might help you solve the problem of getting a return response to your Ajax call, there is no doubt that you will run into problems using this solution. For one, if there are any long processes in a JavaScript code, it will lock the user interface and make it unresponsive.
When JavaScript is used in conjunction with XML or REST APIs, you can create some useful behaviors with a set of web-development techniques collectively known as Ajax. Let’s take a look at a specific Ajax functionality: returning an Ajax response from an asynchronous JavaScript call.
For instance, you call somebody you want to talk to, but the person is not available, so you leave a message to have him or her call you back. This way, you no longer have to wait on the phone listening to the hold music, you can do other things until the person returns your call. Ajax requests do just that.
use strip_tags while sending data from server file if that is in php like below-
<script src="jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$.ajax({
type: "POST",
url: "test2.php",
contentType: "application/json; charset=utf-8",
success: function(result) {
//alert(result);
$("#commmonname").html(result);
}
});
});
</script>
<div id="commmonname"></div>
SERVER file
<?php
$msg="<h2>HI</h2>";
echo strip_tags($msg);
?>
I tried a lot and finally i got the solution using below code.
$.ajax({
type: "POST",
url: "page name/method name",
data: '{ param 1: "value", param 2: "value" }',
contentType: "application/json; charset=utf-8",
success: function (data) {}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With