[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html>
<head>
<title>POST请求</title>
</head>
<body>
<button>POST</button>
<div id="response"></div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("button").click(function() {
$.ajax({
url: "/items/",
type: "POST",
contentType: "application/json",
headers: {
"Host": "127.0.0.1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat",
"Authorization": "TOKEN"
},
data: JSON.stringify({
"name": "example item",
"price": "123456",
}),
success: function(response) {
console.log(response);
// 在页面上显示响应结果
$("#response").html(JSON.stringify(response));
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
// 在页面上显示错误信息
$("#response").html("Error: " + xhr.responseText);
}
});
});
});
</script>
</html> |