0

I have (still error resp = undefinited). I test request return data ok. Because I have a little skill in JavaScript, I need a small step to set data from response of request.

    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "https://localhost:8443/control/jqxGeneralServicer?sname=JQGetListCustomerByCluster&deliveryClusterId=120120", true);
    xhttp.setRequestHeader('Content-Type', 'application/json');
    xhttp.send();

    var resp;
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
        resp = this.responseText        
        }
    }
    console.log("resp = " + resp);

I want resp has data inside it, how to do it?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Access it inside the callback or wait for it. By the time you call `console.log()` it is still unset, due to the request being asynchronous. Also I recommend to `send` after assigning the `onreadystatechange` event. – Lain Dec 07 '20 at 07:53
  • I need move data outsite of `xhtp.onreadystatechange` for manipulation, it is my main purpose. – Vy Do Dec 07 '20 at 07:54
  • Seemly no. I want save response data to a variable or something for storing data. It can use outsite `xhttp.onreadystatechange` , an persiting it. – Vy Do Dec 07 '20 at 07:57
  • 1
    You can very well assign the response value to an outer scope variable in the callback. Only that it's not accessible before the value is loaded, and you need a way to somehow time the access to that variable. The easiest way is to call a function from the callback, then you won't need an outerscope variable, though. Asynchronous programming provides changes to the program flow, you've to design your program architecture asynchronous from the beginning, it's not possible to use synchronous structures. In other words, you can't eat an unbaked pizza. – Teemu Dec 07 '20 at 08:01

2 Answers2

0

I think you not see data in console log because of asynchronous. Modify a bit like this and you will see data be assigned.

var resp;
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        resp = this.responseText;
        console.log("resp = " + resp);
    }

}
Plumpboy
  • 45
  • 1
  • 7
  • I need move data outsite of `xhtp.onreadystatechange` for manipulation, it is my main purpose. – Vy Do Dec 07 '20 at 07:54
  • i know, so u need something like callback or promise to deal with onreadystatechange function. – Plumpboy Dec 07 '20 at 08:05
0

you can access the evt inside your block here:

 xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
        resp = this.responseText        
        }
    }

You can make it:

let resp;
 xhttp.onreadystatechange = function(evt) {
        if (this.readyState == 4 && this.status == 200) {
          resp = evt.target.response;      
        }
    }

MarketMan39
  • 188
  • 1
  • 7
  • this is my code https://gist.github.com/donhuvy/c5628f57e6d10c3d4b86a91cef8c7238#file-deliveryclusterdetail-ftl-L201 like you , and result https://user-images.githubusercontent.com/1328316/101324442-d8e6f380-389c-11eb-84bf-f65966debcd0.png The request is ok, I tried it, it return data in well-form. – Vy Do Dec 07 '20 at 08:00
  • you have `xhttp.onreadystatechange = function() {` ..you need `evt` like this: `xhttp.onreadystatechange = function(evt) {` – MarketMan39 Dec 07 '20 at 08:01
  • I do like you guide https://user-images.githubusercontent.com/1328316/101324876-73473700-389d-11eb-8f60-c8a39318a333.png , but still error https://user-images.githubusercontent.com/1328316/101324939-8c4fe800-389d-11eb-9f3b-a18ad336c4b8.png – Vy Do Dec 07 '20 at 08:04