jQuery Ajax passing response variable to function

Samik Sengupta

I have a Javascript function that uses jQuery Ajax to fetch response from a Servlet. It works when I use the anonymous function method-

function doAjax() {
    $.ajax({
        url : 'Authentication',
        data : { userName : 'poko' },
        success : function(data){
            alert(data);                               //this works
        }
    });
}

But when I try to pass the response variable to a declared function nothing happens.

function doAjax() {
    $.ajax({
        url : 'Authentication',
        data : { userName : 'poko' },
        success : showResult(data)                     //this doesn't
    });
}

function showResult(d) {
    alert(d);
}

Firefox debug gives a ReferenceError: data is not defined. Can I actually get the second method to work?

AmmarCSE

In the second attempt

function doAjax() {
    $.ajax({
        url : 'Authentication',
        data : { userName : 'poko' },
        success : showResult(data)                     //this doesn't
    });
}

you are actually executing showResult and then assigning its result to the success handler. What success expects is an anonymous function or a function reference(without passing parameters to it).

What you can do is

function doAjax() {
    $.ajax({
        url : 'Authentication',
        data : { userName : 'poko' },
        success : showResult                  
    });
}

and the data will automatically be passed in as the first parameter to the function referenced.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery Passing variable to a function

From Dev

Passing variable to a Jquery function

From Dev

save ajax response to jquery variable

From Dev

Passing jquery function as variable parameter to the custom function

From Dev

Ajax Jquery Passing variable to PHP file

From Dev

passing variable from jQuery ajax to nodejs

From Dev

Passing direction as a variable to jquery animate function

From Dev

Select element by passing variable in function - jQuery

From Dev

Passing a jQuery load() function variable to codeigniter controller

From Dev

passing jquery variable to php function as parameter

From Dev

JQuery AJAX HTML response in error function

From Dev

Call function in jQuery from AJAX response

From Dev

JQuery AJAX HTML response in error function

From Dev

jQuery get response from ajax function

From Dev

Passing variable value from one function to another in autocomplete with AJAX call

From Dev

Passing variable data between jQuery and PHP using AJAX shorthand

From Dev

Passing a PHP variable to JavaScript to use in jQuery.ajax

From Dev

Ajax - Modal - Passing Variable

From Java

Pass variable to function in jquery AJAX success callback

From Dev

jQuery Cannot define variable inside AJAX function

From Dev

Jquery ajax function how to pass variable

From Dev

Jquery passing data to ajax function in mvc4

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

Passing parameters to a function in Ajax

From Dev

Passing parameters to a function in Ajax

From Dev

Passing data with jquery ajax

From Dev

Calling PHP file via AJAX, passing $_GET variable into MySQL query, then echoing into response

From Dev

jQuery target html from ajax response using a separate function

Related Related

  1. 1

    jQuery Passing variable to a function

  2. 2

    Passing variable to a Jquery function

  3. 3

    save ajax response to jquery variable

  4. 4

    Passing jquery function as variable parameter to the custom function

  5. 5

    Ajax Jquery Passing variable to PHP file

  6. 6

    passing variable from jQuery ajax to nodejs

  7. 7

    Passing direction as a variable to jquery animate function

  8. 8

    Select element by passing variable in function - jQuery

  9. 9

    Passing a jQuery load() function variable to codeigniter controller

  10. 10

    passing jquery variable to php function as parameter

  11. 11

    JQuery AJAX HTML response in error function

  12. 12

    Call function in jQuery from AJAX response

  13. 13

    JQuery AJAX HTML response in error function

  14. 14

    jQuery get response from ajax function

  15. 15

    Passing variable value from one function to another in autocomplete with AJAX call

  16. 16

    Passing variable data between jQuery and PHP using AJAX shorthand

  17. 17

    Passing a PHP variable to JavaScript to use in jQuery.ajax

  18. 18

    Ajax - Modal - Passing Variable

  19. 19

    Pass variable to function in jquery AJAX success callback

  20. 20

    jQuery Cannot define variable inside AJAX function

  21. 21

    Jquery ajax function how to pass variable

  22. 22

    Jquery passing data to ajax function in mvc4

  23. 23

    How to get the ajax response from success and assign it in a variable using jQuery?

  24. 24

    How to get the ajax response from success and assign it in a variable using jQuery?

  25. 25

    Passing parameters to a function in Ajax

  26. 26

    Passing parameters to a function in Ajax

  27. 27

    Passing data with jquery ajax

  28. 28

    Calling PHP file via AJAX, passing $_GET variable into MySQL query, then echoing into response

  29. 29

    jQuery target html from ajax response using a separate function

HotTag

Archive