0

I'm using an ajax post request to authenticate users, this script replies with a full webpage. Within the response if successful there is a div called #logged.

What I would like to do, is check if the requested result contains logged and then replace content within the corresponding #primary-content div. How can I do it?

Surfer on the fall
  • 721
  • 1
  • 8
  • 34
  • Can you provide the code you have tried so far? Someone will probably get you an answer, but you learn a lot more by trying it yourself, first. – JasCav Sep 19 '12 at 13:55

2 Answers2

1

Do you want to check if ajaresult contains "logged " ? var primaryContent = '';

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    var loggedDiv = null;
    loggedDiv  = $(data).find('div#logged');
    if(loggedDiv != null && loggedDiv != undefined){
      primaryContent  = $(data).find('div#primary-content').html();
    }
  }
});
Pit Digger
  • 9,618
  • 23
  • 78
  • 122
  • Ehm, the problem is that response string contains a full webpage. I want to check if the response contains a div called logged, then get the content of a div called 'primary-content' contained in the response string. – Surfer on the fall Sep 19 '12 at 14:00
1

I want to check if the response contains a div called logged, then get the content of a div called 'primary-content' contained in the response string.

Pretty simple really... Once your ajax object readyState has reached 4.

Just do this... presuming your ajax or data object is called ajax.

var logged = $("#logged", $(ajax.responseText)); //grab the logged element from the ajax obj
//check that the logged element exists
if(typeof logged !== "undefined"){
    //replace the content
    $("#primary-content").html(logged.contents());
}

Sources

Community
  • 1
  • 1
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78
  • ajax responseText contains a full webpage, not only the primary-content div or logged div. I want to work with selectors, using as context the result string. How can I do it? – Surfer on the fall Sep 19 '12 at 13:57
  • There you go, I've edited my answer :P other options would to get your webpage is response with some JSON (much more manageable that way). – classicjonesynz Sep 19 '12 at 14:12
  • 1
    convert the data in the response to .html() then use it like an object. var y = someResponse.html(); y.contains("x"); – TheMonkeyMan Sep 19 '12 at 14:14