Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX returns HTML code with output

Tags:

html

jquery

ajax

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

        }
like image 700
Piyush Avatar asked Mar 03 '15 07:03

Piyush


People also ask

How to return JSON and HTML in Ajax?

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.

Is it possible to get a return response to Ajax call?

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.

What is Ajax and how does it work with JavaScript?

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.

What is an Ajax request?

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.


2 Answers

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);
?>
like image 190
TECHNOMAN Avatar answered Sep 17 '22 02:09

TECHNOMAN


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) {}
});
like image 27
nativegrip Avatar answered Sep 21 '22 02:09

nativegrip