jQuery Ajax Render Fragments OR Whole Page -


i'm in throes of building application wraps mess of legacy code. page working on, need substitute fragment divs constructed on back-end -- or else need replace entire page altogether. idea there dynamically controlled flow needs satisfied before can forward legacy product.

substituting fragments works fine, seen in below my_body_content swap. trouble comes when i'm trying render not fragment, whole page, in "body" swap. @ point page goes blank.

i want similar errors returned server. want nice rest 404 error messages displayed on screen, legacy-product 404s show in legacy-product 404 page.

yes, requirements project weird. that's not problem can fix.

here's jquery invocation, names changed protect guilty:

    $.ajax({     url: "places/things",     type: "post",     data: json.stringify(somebadassobject),     datatype: "text",     accepts: "text/html",     contenttype: "application/json; charset=utf-8",     success: function(x) {         var fragcheck = $("#my_fragment", x);         if (fragcheck != null && fragcheck.length > 0)             $("#my_body_content").html(x);         else             $("body").html(x);     },     error: function (xmlhttprequest, textstatus, errorthrown) {         error(errorthrown);         if (xmlhttprequest.responsetext.startswith("<html>"))             $("body").html(xmlhttprequest.responsetext);     } }); 

okay, think can answer own question:

$("body").html(x); 

doesn't work, and

$("html").replacewith(x); 

doesn't work, , other permutations along same idea don't work either.

this works:

document.open(); document.write(xmlhttprequest.responsetext); document.close(); 

so whole solution looks this:

$.ajax({ url: "places/things", type: "post", data: json.stringify(somebadassobject), datatype: "text", accepts: "text/html", contenttype: "application/json; charset=utf-8", success: function(x) {     var fragcheck = $("#my_fragment", x);     if (fragcheck != null && fragcheck.length > 0)         $("#my_body_content").html(x);     else     {         document.open();         document.write(x);         document.close();     } }, error: function (xmlhttprequest, textstatus, errorthrown) {     error(errorthrown);     if (xmlhttprequest.responsetext.startswith("<html>"))     {         document.open();         document.write(xmlhttprequest.responsetext);         document.close();     } } 

});

hope helps next poor schmoe!


Comments

  1. Thanks for suggesting hiring via outsourced experts. I also preferred the same thing a month ago. I hired some java spring framework experts through an amazing platform named Eiliana.com. Now, I am very happy with the work of those employed, and I recommend the same to my fellow business owners. They are actively looking to hire professional experts in technical niches.

    ReplyDelete
  2. Agencies are also concentrating their efforts on world777 gaming, which is expected to be worth $300 billion by 2022. India accounts just a fragment of this, with a market share of about 1%. Given that Indian gambling companies spent Rs 1400 crore on advertising in 2021 alone, the figure is nothing to sneeze at.

    ReplyDelete

Post a Comment

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -