1

I have this code with an ajax call.

var value = 0;
$.ajax({
url: 'http://localhost:3000',
dataType: "json",
type: 'POST',
data: formdata,
success: function(data, textStatus, jqXHR) {
    value = data.data;
    console.log("Inside: " + value);

},
error: function(jqXHR, textStatus, errorThrown) {
    console.log("Error received");
}
});
console.log("Outside: " + value);

Executing, gives me

Outside: 0
Inside: 100

Is there anyway to make it run sequentially?

1
  • Just put your console.log("Outside: "+value) code inside the success callback, after the "Inside" thing? Commented Aug 21, 2015 at 9:56

1 Answer 1

2

From what I understand, your question has nothing to do with Express or EJS. You want synchronous ajax in the browser, which is certainly possible. If you're using jQuery, you need to set the async property to false, as per this documentation:

http://api.jquery.com/jquery.ajax/

It is, however, not recommended to do that, as explained there.

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

Comments

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.