Solved

Call load() function after ajax Success

Posted on 2016-10-07
4
30 Views
Last Modified: 2016-10-15
I am trying to do a partial page re-load after my ajax POST returns successful. I have tried putting the load() function in the Success callback but it never gets triggered. I know it's not a problem with the load() function itself because I can run it in the console and it works fine. How do I run it on success? Thanks.

        $("#saveNewTask").click(function () {
            $.ajax({
                cache: false,
                url: url,
                data: data,
                type: "POST",
                error: function (e) {
                    console.log(e);
                    alert("Error creating new task");
                },
                success: function (response) {
                    $('#mainTaskWrapper').load('/Tasks #mainTaskWrapper');
                }
            });
        });

Open in new window

0
Comment
Question by:Grant Whiting
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41834567
Are you certain that the success clause is being invoked? Have you placed an alert or console.log statement in the success clause to confirm that it is being invoked? There are situations in which neither the error or success clause will be invoked.
0
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41834704
Nothing wrong with the code
Sample
HTML
<button id="saveNewTask">Save</button>
<div id="mainTaskWrapper"></div>

Open in new window

jQuery
$(function() {
  url = 'reflect.php';
  data = {test: 'test'};
  $("#saveNewTask").click(function () {
    $.ajax({
      cache: false,
      url: url,
      data: data,
      type: "POST",
      error: function (e) {
        console.log(e);
        alert("Error creating new task");
      },
      success: function (response) {
        $('#mainTaskWrapper').load('t1685data.html #mainTaskWrapper');
      }
    });
  });
});

Open in new window

Load HTML [t1685data.html]
<!doctype html>
<html>
<body>
<p>This is outside of the load zone</p>
<div id="mainTaskWrapper">This is the content that will be displayed</div>
<p>This is outside of the load zone</p>
</body>
</html>

Open in new window

Working sample here

Problem must be elsewhere - what are you seeing in the console.
0
 

Accepted Solution

by:
Grant Whiting earned 0 total points
ID: 41835322
I got it figured out. I was making a dumb mistake. The ajax call was in an iframe and the element was in the parent doc. I had to select
#mainTaskWrapper

Open in new window

like
$('#mainTaskWrapper', window.parent.document)

Open in new window

.
0
 

Author Closing Comment

by:Grant Whiting
ID: 41844869
I made a mistake and was set on the right path by Julian Hansen.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question