0

I'm using ajax to return some data from my server. What I get is a data string object.

How can I parse this html object, so that I only get a part of this returned data?

2
  • 3
    Too little information. Try to return the string as JSON from the server and use JSON.parse(stringData) Commented Jan 21, 2011 at 10:32
  • I return html, and try to find a element by id... Commented Jan 21, 2011 at 10:37

1 Answer 1

1

Depends on the dataType. What kind of data do you expect to receive and which dataType do you specify in the jQuery .ajax() method?

If you specfiy json for instance, jQuery (since version 1.3.2 I guess) will try to parse the received data into a Javascript object. But in general, you can just modify the received data yourself with all String methods available.

$.ajax({
    url:  '...',
    dataType: 'text',
    success: function(data) {
        //var part = data.substring(0,8);
        $(data).find('#my_element');
    }
});

This would take only the first 8 characters from the received data. Another way is to apply a regular expression on your received data. All on you.

edit

Based on your comment: If your transfer a valid HTML chunk of data, you can just wrap that data into a jQuery contructor and use all methods available there, for instance .find()

Sign up to request clarification or add additional context in comments.

4 Comments

data.find() gives me a error... how can I convert data into a jquery object then?
I think you should just remove the dataType line, if it is HTML - it properbly yells at you because it don't treat the returned result as HTML. Or maybe the dataType sould be 'html' instead.
Ah I see i use js format for every ajax request. How to change or remove the dataType line?
I've found a way now: $('#buffer').html(data); this way the html gets written in a jquery object which is parsable...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.