toop
toop

Reputation: 51

ajax - return only part of responsetext

The below is just a bit of my ajax code, at the moment my variable 'resp' is returning an entire response from test.php. Is there any way I can just return the responseText between a specified tag? ie. just get back the responseText of the html inside test.php's div of 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);

Upvotes: 5

Views: 2079

Answers (4)

Dipesh KC
Dipesh KC

Reputation: 3297

You could use jquery shorthand function load() to do this in a swift

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

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

is this what you want?

Upvotes: 2

marcosfromero
marcosfromero

Reputation: 2853

Are you trying to update some portion of your HTML with that response? If so, you can use jQuery and call the load method that accepts a special URL parameter to load a portion of the returned HTML:

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

Upvotes: 1

DanielB
DanielB

Reputation: 20240

If you are willing to switch to jQuery you could do the following

$.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();
    }
});

Upvotes: 1

bungdito
bungdito

Reputation: 3620

change your ajax code to:

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

then in the test.php page, write for response like this

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

Upvotes: 1

Related Questions