简体   繁体   English

javascript函数未调用Ajax

[英]Ajax not being called in javascript function

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. 问题在于ajax不会触发,因此永远不会触发和显示数据。 Here is my ajax call with a javascript function call in the success function. 这是我的ajax调用,其中包含成功函数中的javascript函数。

$.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. 我无法弄清楚为什么未触发retrieveUserBlogData()函数中的ajax。 Any help would be appreciated Thanks. 任何帮助,将不胜感激谢谢。

Even if the AJAX succeeds, you are redirecting the browser to a different page after the first AJAX request: 即使AJAX成功,您也会在第一个AJAX请求之后将浏览器重定向到其他页面:

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

So I would suggest removing that. 所以我建议将其删除。

Try the following code for redirecting to another window 尝试以下代码重定向到另一个窗口

window.location.assign(URL);

then it may work. 那么它可能会起作用。

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";
    }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM