-1

I don't know how to implement getState. What I am trying to do is update a field on click with a state value returned in my AJAX call. I have seen a reference to promises in another response but I do not understand how to use them.

    function processRequest(e) {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var response = JSON.parse(xhr.responseText);
            var state = response.state;
        }
    }
    $('#myState').on('click', function() {
            var localState = getState();
            $('#location').val(localState);
    });
Ruby
  • 27
  • 2

1 Answers1

0

You need to change variable scope so it would be accessible from out of processRequest() function.

var response = '';
var state = '';

function processRequest(e) {
        if (xhr.readyState == 4 && xhr.status == 200) {
            response = JSON.parse(xhr.responseText);
            state = response.state;
        }
    }
    $('#myState').on('click', function() {
            $('#location').val(state);
    });
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91