Search code examples
jqueryjquery-loadjquery-post

Combination of .load and .post?


Basically, I want to load the content of a page, while also passing it some POST parameters.

Now, you may say, I use .post, and then just use something like $("#div").html(data), but the problem is I want the functionality of the only-select-one-element thing in .load. Can that be used with .post somehow, perhaps?


Solution

  • No, there is no method for loading content with a POST request.

    You can make a POST request and place the result in the page. Example:

    $.post(
      'page.html',
      { post: 'data' },
      function(data){
        $('#someElement').html(data);
      },
      'html'
    );
    

    Edit:

    To use a partial result of the returned code, you can parse the result just as the load method does:

      function(data){
        var c = $(data);
        $('#someElement').append(c.find('#somePart'));
      },
    

    Edit 2:

    As Jack showed, there is actually a way to make the load method make a POST request. (I missed it because of the misleading documentation.)

    Example with a partial use of the result, sending the data as an object triggers the POST request method:

    $('"someElement #somePart').load('page.html', { post: 'data' });