用普通JavaScript向服务器发送GET请求的最佳方式是什么?

用普通JavaScript向服务器发送GET请求的最佳方式是什么?,javascript,node.js,get,Javascript,Node.js,Get,用普通JavaScript向服务器发送GET请求的最佳方式是什么?您可以通过重定向来执行同步GET请求: var url = 'http://domain/path/?var1=&var2='; window.location = url; 你可以用Fetch试试 函数请求(){ 取('http://example.com/movies.json') .然后(功能(响应){ log(response.json()) }) .then(函数(myJson){ log(myJson);

用普通JavaScript向服务器发送GET请求的最佳方式是什么?

您可以通过重定向来执行同步GET请求:

var url = 'http://domain/path/?var1=&var2=';

window.location = url;
你可以用Fetch试试

函数请求(){
取('http://example.com/movies.json')
.然后(功能(响应){
log(response.json())
})
.then(函数(myJson){
log(myJson);
});
}
request()
使用(XHR)对象

代码示例:

const http = new XMLHttpRequest();
const url='/test';
http.open("GET", url);
http.send();

http.onreadystatechange = (e) => {
  console.log('done')
}

在香草javascript中,您可以使用


我不确定我们是否能在这里提出“最佳方式”, 但是你可以用

或者如果你想使用图书馆


当你说vanilla JS时,你的意思是不使用任何软件包吗?这是否回答了你的问题?这与python请求有什么关系?浏览器js和您也使用的
node.js
标记也有很大不同。另外,对于“最佳方式”,您使用什么标准?虽然这是正确的,但原始XHR API已经过时。现代的方法是使用fetch,它更优雅,也支持开箱即用的承诺。虽然这是正确的,但是原始的XHRAPI已经过时了。现代的方式是使用fetch,它更优雅,也支持开箱即用的承诺。
const http = new XMLHttpRequest();
const url='/test';
http.open("GET", url);
http.send();

http.onreadystatechange = (e) => {
  console.log('done')
}
fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });