I have a page with one form and two possible responses in the event of a successful AJAX call, one of which only returns a status code.
What I need to do is check the response
object in my success callback for any HTML contents so that I can display them on my page.
I already know that I can access response
in my callback by adding it as a parameter, like so:
function success(response) { }
The only thing I can't figure out is how to check if that object has any HTML contents. How can I do this?
I have a page with one form and two possible responses in the event of a successful AJAX call, one of which only returns a status code.
What I need to do is check the response
object in my success callback for any HTML contents so that I can display them on my page.
I already know that I can access response
in my callback by adding it as a parameter, like so:
function success(response) { }
The only thing I can't figure out is how to check if that object has any HTML contents. How can I do this?
Share Improve this question asked Jul 25, 2013 at 17:34 keeehlankeeehlan 8,05416 gold badges58 silver badges105 bronze badges 5 |1 Answer
Reset to default 24You probably want to look at the response headers for an HTML MIME type. $.ajax
will pass a jqXHR
object back into your success
callback, which you can then call .getResponseHeader()
on:
function success( response, status, jqXHR ) {
if( jqXHR.getResponseHeader('content-type').indexOf('text/html') >= 0 ) {
...
}
}
if(response.indexOf('<') > -1)
because status codes don't have<
but html does. – Ohgodwhy Commented Jul 25, 2013 at 17:39echo json_encode(array('success' => true, 'html' => '<html stuff>'))
when successful, otherwise,echo json_encode(array('success' => false))
when not successful and check ifresonse.success !== false
– Ohgodwhy Commented Jul 25, 2013 at 17:42