ajax--how to save the obtained response into a global variable?

     This essay seems to be a while away from the last one. Why did it take so long to write? It’s not the same as I think. This is probably human nature, hahaha, what is this actually today? This probably needs an opportunity (I added a supervision group, everyone is sorry for not writing). 

   No matter what, I want to persist in my heart, Please believe me, I can.

 Today, let's just record a little knowledge from the previous few days, at least for me.

  To get the response in ajax and put it into a global variable, I first thought of using web storage, but under IE10, I found that it can't be used locally, and it doesn't seem to support it? Well, Du Niang, put it in the server environment, ok can solve it. But it seems a bit uncomfortable if you want to watch it locally, okay, let's look at other ways.

  

function getData(){ 
var data
var xhr=new XMLHttpRequest();
xhr.open("GET","http://116.62.180.61:31002/points/data/tranlist?page=1&pagesize=10",false);
xhr.onload=function(){
data=JSON.parse(this.responseText).data.count;
}
xhr.send();
return data;
}

var first = getData(); 

Let’s use a closure, and return the data inside to get it; with this native one, you can get it, but when I wrote it, $.ajax() was used for convenience. I didn’t expect that it gave me a bomb below ie10. undefined. I tried it natively and got it, and then I printed the error in $.ajax() and found the problem. It was because $.ajax() went error below ie10, and the responseText given was no transport, so it was undefined. I went to Du Niang again and found that adding jQuery.support.cors = true; before the code can solve that problem. I know it but I don't know why. Although the problem is solved, I don't know why adding this can solve it. Later, I checked and said that it is because different objects ie are XDomainRequest and others are XmlHttpRequest. Due to different objects, xhr.withCredentIals = true needs to be set; although I still don't understand it after reading it, I think the code added on JQ and this should be It's almost the same! (If you know, please correct and guide, thank you)

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324665282&siteId=291194637