5

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);

        }
4
  • 1
    change your serverside script to change the returned value Commented Mar 3, 2015 at 7:26
  • Didn't work. Exactly what you want me to do? Commented Mar 3, 2015 at 7:28
  • check whether the script is error free or not? Commented Mar 3, 2015 at 7:42
  • Can u add your server side script here? Commented Mar 3, 2015 at 7:59

3 Answers 3

6

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);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I have added server script please take a look.
u took the wrong variable ..see $saa and $aaa. Also plz give the data which is coming from the database so we can see its format..
How to validate that a query to the database returns results validate with if in the AJAX response?
2

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) {}
});

Comments

2
$.ajax({
    URL: "<?= base_url()?>/controller name/function name",  
    type: 'POST',
    data: {school_code:school_code,class_name:class_name},
    contentType: "application/json; charset=utf-8",
    success: function(res)
    {
        console.log(res);
    }
});

Actually the real problem in this case is URL. if the URL is not set properly then function will not call and Ajax will return the same page content in response. so please check your URL and set it like above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.