0

I am making a jquery post request to obtain a part of the html code from server. There is a file on server get_info.php which prints html code for different requirements. I am using following code to do this :

function check(inf_type) {
    $.ajax({
        type: 'POST',
        url: "get_info.php",
        data: { "sequence_no" : 1 },
        success: function(data) {
            // how can i use value of variable "inf_type" here.
            // here, the variable "data" contains HTML code.
        },
        dataType: 'text'
    });
}

the function check() accepts a parameter inf_type which contains random strings according to which, server recognize the html code to print. Now, i want handle the POST response according to this inf_type. How can i access the value of inf_type variable in POST response function? The function check() is called more often, thats why i can not put the inf_type variable value in any global variable. What can i do to achieve that? Please guide me. thanks in advance.

Vinay Jeurkar
  • 3,054
  • 9
  • 37
  • 56
  • 1
    “Closures are poor man's objects”. Learn about `Closures`. I mean in javascript. https://developer.mozilla.org/en/JavaScript/Guide/Closures –  May 25 '12 at 05:22
  • Not related to your question, but why a `POST` request to *get* HTML response? – Niks May 25 '12 at 05:26
  • @NikhilPatil: That is just a filename. I need to send even different and larger data to server through that request. – Vinay Jeurkar May 25 '12 at 05:39

4 Answers4

1

You can use the info_type variable in the Success function. scope of parameter info_type is still exists in your success function.

Sudhakar B
  • 1,465
  • 9
  • 16
  • inf_type is not a global variable and the check() function is called, lets say, 2 times in a second with 2 different values of inf_type. In this case the requests and values should not be mixed. – Vinay Jeurkar May 25 '12 at 05:22
  • 1
    No it wont be mixed.For each function call info_type will have its own instance value. You can use. – Sudhakar B May 25 '12 at 05:24
1

You can directly use the variable in success or in error function.

function check(inf_type) {
    $.ajax({
        type: 'POST',
        url: "get_info.php",
        data: { "sequence_no" : 1 },
        success: function(data) {
            alert(inf_type); //inf_type is available here.
        },
        dataType: 'text'
    });
}
Sumit
  • 2,932
  • 6
  • 32
  • 54
1

You can access it via inf_type parameter of the check() function:

function check(inf_type) {
    $.ajax({
        type: 'POST',
        url: "get_info.php",
        data: { "sequence_no" : 1 },
        success: function(data) {
            if (inf_type == 0) {
                // do something with data
            } else {
                // do something else
            }
        },
        dataType: 'text'
   });

}

The reason why this works is because the inner function (success callback) has access to variables in the outer function (check). See this answer for more details: https://stackoverflow.com/a/111200/69868

Edit This assumes that inf_type is a either a number or a new (different) instance of object in each call of check(). Details are explained in the link mentioned above.

Community
  • 1
  • 1
Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99
  • inf_type is not a global variable and the check() function is called, lets say, 2 times in a second with 2 different values of inf_type. In this case the requests and values should not be mixed. – Vinay Jeurkar May 25 '12 at 05:22
  • 1
    @Forte That's the point! Since inf_type is not a global variable, it is local for the invocation of check() function. Therefore the success callback will use this local value, always the right one. Please do read the link I gave you for longer explanation. – Miroslav Bajtoš May 25 '12 at 05:27
0

Firstly you have to send the inf_type to the server for the specific values to return like this :

function check(inf_type) {
 $.ajax({
    type: 'POST',
    url: "get_info.php",
    data: { "sequence_no" : 1, whatToSearch : inf_type }, // i added
    success: function(data) {
         //$(selector).html(data);//where you want to show the data
        // how can i use value of variable "inf_type" here.
        // here, the variable "data" contains HTML code.
    },
    dataType: 'text'
 });
}
Ankur Verma
  • 5,793
  • 12
  • 57
  • 93
  • because the server is sending HTML code in response, i was looking for another way which is more short and perfect. thank you for your help. – Vinay Jeurkar May 25 '12 at 07:03
  • sending inf_type to server and again taking it back, then searching in response data for the value because its a HTML code patch.. this involves lot of iterations. I just wanted to keep my js codes simple and easily understandable. – Vinay Jeurkar May 26 '12 at 02:18
  • so you every time takes full data from the server and then sorts it out by javascript, why don't you send the **inf_type** in request and pulls data from server which is relevant to it and sets the returned data directly to the respective div or span...... – Ankur Verma May 26 '12 at 08:57