如何能在xmlhttp 同一层级,打印出result这个值
将 xmlhttp 请求换成同步方式,默认 true 是异步,设置成 false :
xmlHttp.open(type, url, false);//同步方式请求
这个代码是异步的,因此console.log执行的时候,服务器还没有返回。
var xmlhttp =new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","https:// ",true);
xmlhttp.send();
$.ajax({
url:"http://",
success: function(data){
$("#sh").html(data);
}
});
把json格式化先去了,有可能数据根本不符合json规范转不过来