?
Solved

Call load() function after ajax Success

Posted on 2016-10-07
4
Medium Priority
?
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

718 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