Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Call load() function after ajax Success

Posted on 2016-10-07
4
Medium Priority
?
46 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 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

963 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