1

I have the following JQuery Ajax method:

$.ajax({
    type: 'POST',
    url: $(form).attr('action'),
    data: formData,
    dataType: 'json',
    success: function() {
        $('#test').html("testing123");
    },
});

As it is written, the success function does not fire.

However, if I predefine the function somewhere else and then call it like this:

success: testFunction()

OR

success: $('#test').html("testing123")

then it works.

What am I missing?

1
  • i think you have to pass testing123 inside function as a Parameter Commented Oct 11, 2015 at 5:58

1 Answer 1

4

The solutions that you think you have working are actually only illusions of so. They are actually not being called on success, but right when you declare it. This leads me to think that your ajax call is not returning success.

The right way to predefine a function and pass it would be

success: testFunction

If you do success: testFunction(), you are running that function right away when parsing the code and not when the callback from your ajax fires. This is the same case with

success: $('#test').html("testing123")

and is actually equivalent if you called testFunction() right after your ajax call.

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.