JavaScript用POST方式发送请求

发布时间:2023-08-24编辑:佚名阅读(338)

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://example.com/api/data', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.timeout = 5000;
xhr.onerror = function() {
  console.log('Error');
};
xhr.ontimeout = function() {
  console.log('Timeout');
};
xhr.onload = function() {
  if (xhr.status === 200) {
    console.log(xhr.response);
  } else {
    console.log('Error: ' + xhr.status);
  }
};
xhr.send(JSON.stringify({name: 'John', age: 30}));


  关键字:JavaScriptPOST方式发送请求


鼓掌

0

正能量

0

0

呵呵

0


评论区