javascript - Ajax validate response data that is not an empty page -
i'm using ajax populate page (server side) fetch content , display in main window. not have problems ajax, lot of time reponse data empty page <html></html>
.
when check in console ->network->page.html ,
status code ok
response headers
- http/1.1 200 ok
- pragma: no-cache
- refresh: 1; url=http://balbla.com/page.html
- connection: close
- content-type: text/html
then in response tab - > <html></html>
my question how can validate in success function data not empty html page, before running commands below, otherwise, end blank page.
$.ajax({ type: "post", url: 'http://balbla.com/page.html', data: 'country=' + user_country, success: function(data) { $pagewrap.fadeout(1000, function() { $('link[href="css/main.css"]').attr('href', $css); $pagewrap.hide().html(data).fadein(500); $.getscript($scripts); }); }, error: function(xhr, textstatus, error) { alert('error fecthing page') ; } });
thanks
you can strip html tags off response string , calculate it's length.
html tag strip function taken from: strip html text javascript
function strip(html) { var tmp = document.createelement("div"); tmp.innerhtml = html; return tmp.textcontent || tmp.innertext || ""; }
then in ajax function:
success: function(data) { if(strip(data).length > 0) { // content not empty }
Comments
Post a Comment