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

set variable in ajax success
how to store ajax response in javascript variable
use variable outside the success function from an ajax/jquery call
how to increase ajax response time in jquery
how to get value from ajax response in javascript
how to get value from object in ajax response
access variable inside ajax success
jquery return ajax result into outside variable

Hello guys I have a problem in getting the response from my ajax. If I display it in the console. I can view it. But How do I assign it in a variable?

Here's what I have. In my PHP code I have this

public function checkPassword($password){

            $username = $this->session->userdata('username');
            $validate = $this->members_model->checkPassword($password,$username);

            echo $validate;

}

In my jquery I have this

$('#existing').on('keyup',function(){

            var id = '<?php echo $this->session->userdata("user_id"); ?>';
            var password_url = '<?php echo site_url("member/checkPassword/' +id+ '"); ?>';

            $.ajax({
                type: 'POST',
                url: password_url,
                data: '',
                dataType: 'json',
                success: function(response){

                var g = response;
                if(g == 1){
                    $('#existing_info').html('Password is VALID'); //Doesn't display the VALID if the response is 1. Why?
                }else{
                    $('#existing_info').html('Password is INVALID!');
                }

                }

            });

        });

$.ajax({
        type: 'POST',
        url: password_url,
        data: '',
        dataType: 'json',
        success: function(response){
           var k=response;

           if(k.indexOf("1") != -1)
             $('#existing_info').html('Password is VALID');
           else
              $('#existing_info').html('Password is INVALID!');
        }
    });

response is in response variable of success function.

indexof returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, returns -1 if the value is not found.

Assigning Ajax response to variable, Hello, I am trying to assign an ajax response to a variable. How would I type: '​GET', dataType: 'json', async: false, success : function(data) It'll be easiest for you to use promises, which jQuery has made very convenient. $. I did notice if I run console.log('test1'); on ajax success, then console.log('test2'); in normal js after the ajax function, test2 prints before test1 so the issue is an ajax call has a small delay, but doesn't stop the rest of the function to get results. The variable simply, was not set "yet", so you need to delay the next function.


try something like this

   <script>
        var k = null;
        $(function(){
            $('#existing').on('keyup',function(){
                var id = '<?php echo $this->session->userdata("user_id"); ?>';
                var password_url = '<?php echo site_url("member/checkPassword/' +id+ '"); ?>';

                $.ajax({
                    type: 'POST',
                    url: password_url,
                    data: '',
                    dataType: 'json',
                    success: function(response){
                        if(response == 1){
                            k = response;
                        }
                    }

                });

            });
        })
   </script>

How to Store The $.ajax Response in Some Variable , but i want to use in future so i want to store so i want that whatever the result coming from $.ajax success callback that would be assign to some  Store ajax json response into javascript variable using jquery If you wish to make an ajax call, and assign response json into a variable directlyhere is a small


In your success response you will get what you are set to output in php. If you want to get an array or data set you can encode it in json in your php script like

echo json_encode($validate);

Then in your jquery you can use this response like this

var responseData = jQuery.parseJSON(response);
console.log(responseData);

console.log will print json object in browser console. You can use this json object like this

responseData.some_data

[Solved] How do I save ajax response to global variable for reuse , You can make ajax wait for a response before continuing by adding that variable is only usable *after* the success has been called so you  jQuery Ajax call - Set variable value on success [duplicate] How to assign ajax post response to a variable. Abort Ajax requests using jQuery.


Ajax is asynch so you will have access to it after the ajax method returns:

$('#existing').on('keyup',function(){

            var id = '<?php echo $this->session->userdata("user_id"); ?>';
            var password_url = '<?php echo site_url("member/checkPassword/' +id+ '"); ?>';

            $.ajax({
                type: 'POST',
                url: password_url,
                data: '',
                dataType: 'json'
            }).then(function(response){
              var k;
              if(response == 1){
                k = response;
                //call another function that needs k here
              }
            });
        });

How to Return AJAX Response from Asynchronous JavaScript Call, Learn how to return AJAX response from an asynchronous Using Ajax, a page is able to request things from the server, process the If you are using jQuery, you can easily do this by setting the async option to false. This will make the foo accept a callback, which it will then use as success callback. How can i save ajax response to a global variable in javascript/jquery to be used later. I have tried several methods including saving the response text (data.responseText) returned from the call but at the end it is still empty/undefined.


 $.ajax({
            type: 'POST',
            url: password_url,
            data: '',
            dataType: 'json',
            success: function(response){

               k=response;

            }

        });

Assigning an ajax response to a variable : learnjavascript, Is there a way to assign ajax response data to a variable to be used later. around, but there is so much written on the topic already, starting with the jQuery docs. $.ajax({ type: "GET", url: myApi, dataType: "jsonp", success: function (data​)  I really struggled with getting the results of jQuery ajax into my variables at the "document.ready" stage of events. jQuery's ajax would load into my variables when a user triggered an "onchange" event of a select box after the page had already loaded, but the data would not feed the variables when the page first loaded.


AJAX XMLHttpRequest Server Response, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  To set the variable you can simply assign the value of data to the iconaddy variable, you will have to make one small change when doing that though which is to add the async setting in your ajax


Return JSON response from AJAX using jQuery and PHP, Directly returning an Array is not possible from AJAX as a response you type: '​get Duration: 9:31 Posted: Dec 6, 2019 Hi I need to store an AJAX Response into two variables x and y or into a array. My AJAX response is a array. I am able to see the data but only with and alert into che call. I need the data outside


jQuery: Return data after ajax call success, , but that would freeze up the browser while it's waiting for the response. It is easy. The implementation is highly dependant on the used tools, but one thing is certain: Javascript is one threaded and the response of the AJAX call always should be handled on asynchronously.