View Full Version : Jquery, parsing HTML AJAX response

07-13-2010, 04:15 AM
I'm make a ajax call to a processing PHP file that echos either "success" or an "error message".

When the success is returning, i want Jquery to recognize this and then redirect the browsers instead of displaying the success message.

Here are some snippets. I can only seem to parse to a blank or null.

Jquery code:

type: "POST",
url: "process.php",
data: wfLoginPost(),
success: function(msg){


// both below are either null or blank
//var res = $(msg).find("span.response").text();
//var res = $(msg).find("span.response").html();

if(res == 'Success'){
window.location = 'http://www.google.com';



if($qry[0]['pwd'] == $password){
echo "<span class='response'>Success</span>";
echo "<span class='response'>Username or password did not match</span>";

Here is the contents of the msg response from the ajax post:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<link rel="stylesheet" type="text/css" href="css/reset.css"/>
<link rel="stylesheet" type="text/css" href="css/960grid.css"/>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" type="text/css" href="css/statusmessageQ.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.statusmessageQ-0.5.js"></script>
<span class='response'>Success</span>

07-13-2010, 04:33 PM
Use a RegExp match():

window.location = 'http://www.google.com';

07-13-2010, 09:15 PM
Thanks. I will try that when I get home tonight. Why can I not get the contents of the <span> in the result msg using .text() or .html() though?

07-14-2010, 03:05 AM
.match() worked!

Thanks so much! :thumbsup:

07-14-2010, 11:30 AM
Why can I not get the contents of the <span> in the result msg using .text() or .html() though?
Never head about the text() nor html() methods.

match() is a method of the JavaScript Regular Expressions. More about RegExp in JavaScript:

06-12-2011, 07:29 PM
Why can I not get the contents of the <span> in the result msg using .text() or .html() though?

I am a new to Jquery but I believe your code won't work because your HTML has not yet been added to your document's DOM.

To do add your Ajax response to an id called "placeholderID" within your document use:-


If you give your success html an id of "success" and your failure response and id of "failure" then you can assign different classes (styles) according to the results of Ajax validation:-


06-13-2011, 05:53 AM
Never head about the text() nor html() methods.

those are both functions defined in jquery, html() returns the html markup for the selected element, text() returns the innerText