0

how do I get the return value of "res" from the function. It shows undefined when i try to access it.

function solution() {
  var url = "https://translate.yandex.net/api/v1.5/tr.json/translate",
    keyAPI = "abcdefgh"
  var xhr = new XMLHttpRequest(),
    textAPI = "some text",
    langAPI = "fr"
  data = "key=" + keyAPI + "&text=" + textAPI + "&lang=" + langAPI;
  xhr.open("POST", url, true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.send(data);
  xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var res = this.responseText;
      return res;
    }
  }
}
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • move return res out of if statement – guradio Feb 23 '17 at 11:06
  • You cant use the `return` to return the result of an asynchronous request from function.. – Zakaria Acharki Feb 23 '17 at 11:09
  • I want the function "solution" to return the value of "res". How do i store the return value in a variable outside the function ? – Ehtesham Shareef Feb 23 '17 at 11:10
  • You can't, that's not how asynchronous code works. See duplicate for more info: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 – freedomn-m Feb 23 '17 at 11:12
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – freedomn-m Feb 23 '17 at 11:12
  • is there any other way i could get the value of res from the function to be used outside of it ? i just want the response from the xhr to be extracted as a string from the function. – Ehtesham Shareef Feb 23 '17 at 11:17

2 Answers2

1

You've to use either Promise or callback approach to achieve that. Promise is relatively new and not supported in all browsers.

Promise approach

function solution() {
  return new Promise(function(resolve, reject) {
    var url = "https://translate.yandex.net/api/v1.5/tr.json/translate",
      keyAPI = "abcdefgh"
    var xhr = new XMLHttpRequest(),
      textAPI = "some text",
      langAPI = "fr"
    data = "key=" + keyAPI + "&text=" + textAPI + "&lang=" + langAPI;
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(data);
    xhr.onreadystatechange = function() {
      if (this.readyState == 4) {
        var res = this.responseText;
        this.status == 200 ? resolve(res) : reject('error');
      }
    }
  });
}

How to get the response

solution().then(function(res) {
  console.log(res);
}, function(err) {
  console.log(err);
});

Callback approach

function solution(success, failure) {
  var url = "https://translate.yandex.net/api/v1.5/tr.json/translate",
    keyAPI = "abcdefgh"
  var xhr = new XMLHttpRequest(),
    textAPI = "some text",
    langAPI = "fr"
  data = "key=" + keyAPI + "&text=" + textAPI + "&lang=" + langAPI;
  xhr.open("POST", url, true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.send(data);
  xhr.onreadystatechange = function() {
    if (this.readyState == 4) {
      var res = this.responseText;
      this.status == 200 ? success(res) : error('error');
    }
  }
}

How to get response

solution(function(res) {
  console.log(res);
}, function(err) {
  console.log(err);
});
VJAI
  • 32,167
  • 23
  • 102
  • 164
  • is there any other way i could get the value of res from the function to be used outside of it ? i just want the response from the xhr to be extracted as a string from the function and stored in a variable for string comparison. the console.log() is not displaying anything – Ehtesham Shareef Feb 23 '17 at 11:26
0

You can try this way. Hope its works-

So your XHR function have one callback function to get the return value,

function solution(callback) {
  var url = "https://translate.yandex.net/api/v1.5/tr.json/translate",
      keyAPI = "abcdefgh";
  var xhr = new XMLHttpRequest(),
      textAPI = "some text",
      langAPI = "fr";
  data = "key=" + keyAPI + "&text=" + textAPI + "&lang=" + langAPI;
  xhr.open("POST", url, true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.send(data);
  xhr.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
       var res = this.responseText;
       callback(res);
     }
   }
}

when you are calling the XHR function:

solution(function(response){
  // you will get the response over here 
});
TechVision
  • 269
  • 1
  • 11
  • is there any other way i could get the value of res from the function to be used outside of it ? i just want the response from the xhr to be extracted as a string from the function. – Ehtesham Shareef Feb 23 '17 at 11:26