Ajax - returns only part of answeretext

Below is just part of my ajax code, while my 'resp' variable returns the whole response from test.php. Is there a way that I can simply return a responseText between the specified tag? i.e. just return the text html answer inside test.php div id = "xyz"?

var loc="test.php?myinput=apple";
ajax.onreadystatechange=function() {
        if (ajax.readyState==4  && ajax.status == 200) {    
            var resp =  ajax.responseText;
        }
    }
ajax.open("GET",loc,true);
ajax.send(null);
+5
source share
4 answers

You can use the jquery shorthand function load () function to make it fast

$('#result').load('test.php' #container');

Here is the result: the div where you want to place the response text and the container is the location of the source test.php element you specified

Is this what you want?

+2
source

change your ajax code to:

if (ajax.readyState==4  && ajax.status == 200) {    
   eval(ajax.responseText);
}

test.php

echo "document.getElementById('xyz').innerHTML='abc';";
+1

jQuery,

$.ajax({
    url: 'test.php?myinput=apple',
    method: 'POST',
    success : function( data ) {
        var content = $(data).find('#xyz').text();
        // alternativly the HTML
        // var content = $(data).find('#xyz').html();
    }
});
+1

Are you trying to update part of your HTML with this answer? If so, you can use jQuery and call loadthat takes a special parameter URLto load part of the returned HTML:

Working example: http://jsfiddle.net/marcosfromero/P5KTv/

+1
source

All Articles