51Testing软件测试论坛

标题: 浏览器测试post请求看大神是怎么做的 [打印本页]

作者: lsekfe    时间: 2022-12-16 10:53
标题: 浏览器测试post请求看大神是怎么做的
需要的代码如下(注意修改url和参数):
  1. var url = "http://localhost:7788/user/save";
  2.   var params = {"name":"zhangsan","id":1,"age":16,"address":"南京"}; //此为json格式的参数
  3.   var xhr = new XMLHttpRequest();
  4.   xhr.open("POST", url, true);
  5.   xhr.setRequestHeader("Content-Type", "application/json");
  6.   xhr.onload = function (e) {
  7.   if (xhr.readyState === 4) {
  8.    if (xhr.status === 200) {
  9.    console.log(xhr.responseText);
  10.    } else {
  11.    console.error(xhr.statusText);
  12.    }
  13.    }
  14.   };
  15.   xhr.send(JSON.stringify(params));
  16.   xhr.onerror = function (e) {
  17.   console.error(xhr.statusText);
  18.   };
复制代码
[attach]145985[/attach]
[attach]145986[/attach]
[attach]145987[/attach]





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2