Steve
Steve

Reputation: 313

jQuery AJAX returned result

I'm having an issue with an ajax call using jQuery. I am posting information to the server, and getting back data as expected. The type of data I am getting back is html. Using firebug, if I console.log the data, it shows an object with all my tags. I want to manipulate the form of the returned data, but when I try to console.log the form, I get an empty object. What am I doing wrong? Here is my code:

$.post('add', {'ajax':true}, function(data){  
  var $data = $(data);  
  console.log($data.find('form'));  
});

Upvotes: 3

Views: 236

Answers (3)

Ali
Ali

Reputation: 267287

The problem is with this line:

var $data = $(data);

If you remove it, you will have the html code returned by server as a normal string variable inside data.

Upvotes: 0

TelegramSam
TelegramSam

Reputation: 2790

I usually prefer the following syntax for selecting elements out of an HTML response:

$.post('add', {'ajax':true}, function(data){
  var myform = $('form', data); 
  console.log(myform);  
});

The second argument to the $() method is used as the context in which to search.

Upvotes: 1

cambraca
cambraca

Reputation: 27837

Look at this example. It works as expected. Maybe your response is not good?

Upvotes: 1

Related Questions