直接引用,修改接口地址及传参即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="submit">jsonp请求</button>
<script>
function getName(data) {
console.log(data);
}
document.querySelector('#submit').onclick = function () {
console.log('ok');
var xhr = new XMLHttpRequest()
xhr.open('post', 'http://127.0.0.1:8009/test')
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
xhr.send(JSON.stringify({
"name": "admin",
"password": "admin"
}))
xhr.onload = function () {
console.log(xhr.responseText);
}
}
</script>
</body>
</html>