sdfh54nf5
sdfh54nf5

Reputation: 45

Ajax doesn't send variable

I have trouble with Ajax. It doesn't send variable "xxx" to file id.php. Code:

var xxx;
$.ajax({
    url: "id.php",
    success: function(result1) {
        xxx = result1;
    }
});

$.ajax({
    url: "check.php", 
    data: "ids="+xxx,
    type: "post",
    success: function (result) {
        .........
        .........
    }
});

Why it doesn't work?

Upvotes: 1

Views: 434

Answers (2)

bluetoft
bluetoft

Reputation: 5443

It doesn't work because your second request is getting called before the first request completes.

you should have your second ajax request be wrapped in the success property of the first call.

success: function(result1){

    $.ajax({
        url: "check.php", 
        data: "ids="+result1,
        type: "post",
        success: function (result) {
            .........
            .........
        }    
    });

} 

Upvotes: 4

pmandell
pmandell

Reputation: 4328

Incorrect format of data. Use JSON format:

data: {"ids" : xxx},

Upvotes: 0

Related Questions