locked
Loading/Updating Content with jQuery RRS feed

  • Question

  • User-1245426334 posted

    Ok... So I'll admit: AJAX wasn't covered when I was getting my degree, so I'm mostly self-taught.

    In the past, I've used actual jQuery AJAX calls ($.ajax({...});), but I recently was reading up about $("...").load() and was a bit intrigued.

    They seem to do basically the same function, but .load() by-passes injecting the returned content into an element.

    So now I'm just wondering if there are specific times to use one over the other and any other insights people may have.

    Thanks!

    -Eric

    Wednesday, April 4, 2012 8:32 PM

Answers

  • User1779161005 posted

    $("#foo").load(url) is just a warapper on:

    $.ajax({url:url,

              type:"GET",

              dataType:"html",

              success : function(result) {

                   $("#foo").html(result);

              });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 4, 2012 8:36 PM

All replies

  • User1779161005 posted

    $("#foo").load(url) is just a warapper on:

    $.ajax({url:url,

              type:"GET",

              dataType:"html",

              success : function(result) {

                   $("#foo").html(result);

              });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 4, 2012 8:36 PM
  • User-1245426334 posted
    Ok. That's what I thought. Thanks! So if I'm looking to get content from a rendered control, I should continue using $.ajax to post data to WebMethods? One other question: I generally return data as json, but I know I can return it as HTML, so my question is when do I use which? Does it even matter? In the example I'm thinking of, the WebMethod would be returning a string of HTML is what would be returned.
    Wednesday, April 4, 2012 8:48 PM
  • User1779161005 posted

    Ok. That's what I thought. Thanks! So if I'm looking to get content from a rendered control, I should continue using $.ajax to post data to WebMethods?

    Well, it works, right?

    One other question: I generally return data as json, but I know I can return it as HTML, so my question is when do I use which? Does it even matter? In the example I'm thinking of, the WebMethod would be returning a string of HTML is what would be returned.

    $().load assumes html and merges the html into the element in the selector. If it's JSON then you can just use $.ajax to work with the results.

    Wednesday, April 4, 2012 8:54 PM
  • User-1245426334 posted
    Well, ya. But working doesn't always mean its following best practices. :P I guess the piece I'm missing is the difference between returning HTML and JSON. Maybe there really isn't any difference, but I'd think there was since we can specify a difference. Thanks for your help, man!
    Wednesday, April 4, 2012 9:05 PM
  • User1779161005 posted

    Well, ya. But working doesn't always mean its following best practices.

    Sinful term! "Best practice" requires context. Usually the answer is "it depends" and thus there's rarely "the one and only one best practice" which is what people are usually looking for.

    I guess the piece I'm missing is the difference between returning HTML and JSON. Maybe there really isn't any difference, but I'd think there was since we can specify a difference. Thanks for your help, man!

    Ah, ok... so yes, there's a difference there. If your Ajax call returns HTML this means you've done the UI "work" (so to speak) on the server (using WebForms, Razor, whatever) and then that HTML is returned to the browser and merged into the DOM (and merged into the DOM in one place). This might work fine, but sometimes things are more complex and this is where returning JSON might make more sense.

    Returning JSON means the UI "work" (rendering) is done in JavaScript/jQuery client-side. The network bandwith is much less (JSON is more compact than HTML [or XML for that matter]). But the downside is this requires more JavaScript to do the rendering (but less server side coding). Returning JSON and thus allowing the JavaScript to build the UI from the result also allows for more complex UIs -- imagine where the results means you have to update multiple elements. That's hard to do if your'e returning a single block of HTML from the server. With JSON the client-side JavaScript can use that JSON data to update multiple parts of the UI. Also, if the client is a mobile device then it's preferred that you return JSON as to minimize bandwith to save battery on the device.

    Anyway, the answer is "it depends" -- that's the consulting answer :)

    Wednesday, April 4, 2012 9:13 PM
  • User-1245426334 posted
    Ya... That makes sense. I'm a black and white kind of guy. :P So since JSON is more compact, is it still ok to use if the data is rendered HTML? I mean, it obviously works. I think I need to read up more about what JSON essentially is and how to best use it. Right now, I do all the rendering server-side because that's the simplest for me. I'm interested in doing thing client-side since that leverages the client's computer processing and not the server's. That should help in overall speed.
    Wednesday, April 4, 2012 9:38 PM