2

In the success function I want to call a function. The problem is that ajax does not fire, so the data is never triggered and display. Here is my ajax call with a javascript function call in the success function.

$.ajax({
    type: "POST",
    url: "./api/login.php",
    data: dataString,
    cache: false,
    success: function(data){
        if(data){
            //FUNCTION CALL WHEN USER LOGGING IN
            retrieveUserBlogData();

            window.location = "api/home.php";
        }else{
            $('.alert').show();

        }
    }
});

function retrieveUserBlogData(){
    $.ajax({
        type: "GET",
        url: 'retrievePostData.php',          
        data: "",
        dataType: 'json',      
        success: handleData
    });
}

function handleData(data) {
    alert(data);

    var blog_file = data[3];            
    $('#imageDiv')
    .append('<img id="blog_img" src="upload/' +     blog_file + '"><br>');
}

I cant figure out why the ajax in the retrieveUserBlogData() function is not being triggered. Any help would be appreciated Thanks.

1
  • 2
    i think the problem is with 'window.location = "api/home.php"'. Because you are changing the page before the success event of second ajax call,. Commented Jan 21, 2015 at 11:32

3 Answers 3

4

Even if the AJAX succeeds, you are redirecting the browser to a different page after the first AJAX request:

window.location = "api/home.php";

So I would suggest removing that.

Sign up to request clarification or add additional context in comments.

Comments

1

Try the following code for redirecting to another window

window.location.assign(URL);

then it may work.

Comments

0

Try it like this

$.ajax({
type: "POST",
url: "./api/login.php",
data: dataString,
cache: false,
success: function(data){
    if(data){
        //FUNCTION CALL WHEN USER LOGGING IN
        retrieveUserBlogData();            
    }else{
        $('.alert').show();

    }
 }
});

function retrieveUserBlogData(){
$.ajax({
    type: "GET",
    url: 'retrievePostData.php',          
    data: "",
    dataType: 'json',      
    success: function(data){
        alert(data);

        var blog_file = data[3];            
        $('#imageDiv')
        .append('<img id="blog_img" src="upload/' +     blog_file + '"><br>');

        window.location = "api/home.php";
    }
});
}

1 Comment

Try moving the entire ajax call from the retrieveUserBlogData() function to the if(data) condition from the first ajax call, and in the succes from the second call use console.log() to see the response obj if there is any

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.