Solved

Call load() function after ajax Success

Posted on 2016-10-07
4
37 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 57

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)

734 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