Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a AJAX function to post for data processing in an .ashx file.
JavaScript
var b = false;
$.ajax({
    async: true,
    contentType: 'application/json; charset=utf-8',
    type: "POST",
    dataType: 'json',
    data: JSON.stringify(arrays),
    url: "MyHandler.ashx",
    success: function (result) {
        b = true;
    },
    error: function () {
        alert('Error occurred');
    }
});

I want set the value of b after it runs successfully. I set a break point at b = true; if it runs successfully. But the program runs without stopping at the break point. It means I did not get the Post Return actually. What's wrong in my code? Thanks.

What I have tried:

C#
How to get AJAX Post return value?
Posted
Updated 11-Oct-16 5:33am
v2
Comments
Karthik_Mahalingam 11-Oct-16 11:13am    
check the console window for any error message in network tab.
ZurdoDev 11-Oct-16 11:19am    
"But the program runs with stopping at the break point" - Do you mean without?

Put a breakpoint in your success function and one in your error function. Also put one at $.ajax( and make sure it is even getting there. It has to go somewhere.
s yu 11-Oct-16 11:34am    
Yes for 'Without'. Sorry for my miss-typing. Thanks

1 solution

Whatever code you have that uses "b" needs to go in the success event also, or put it in a function that the success event code calls.

JavaScript
success: function (result) {
        b = true;
        process(b);
    },


JavaScript
function process(result){
if (result){
    //
}
}


Your ajax call is asynchronous so your code doesn't halt at the $.ajax line, it continues on to the code after while the ajax call completed in the background. The cheaper alternative is to leave your code as it is and make the ajax call synchronous rather than asynchronous by adding

async: false

to the options.

jQuery.ajax() | jQuery API Documentation[^]
 
Share this answer
 
Comments
s yu 11-Oct-16 13:04pm    
No effect if change async: false from true.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900