Solved

Call load() function after ajax Success

Posted on 2016-10-07
4
22 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 21

Expert Comment

by:Kim Walker
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I made a mistake and was set on the right path by Julian Hansen.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now