<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>mock</title>
</head>
<body>
<button>获取商品</button>
<table>
<tr>
<td>序号</td>
<td>名称</td>
<td>编号</td>
<td>分类</td>
</tr>
<tbody id="tbody">
</tbody>
</table>
<script src="https://cdn.bootcss.com/Mock.js/1.0.1-beta3/mock-min.js"></script>
<script>
var url = "https://www.easy-mock.com/mock/5d257e0a77744b2808b88407/study/goods"
var btn = document.getElementsByTagName("button")[0]
var tbody = document.getElementById("tbody")
var response;
var str = ""
// tbody.innerHTML="nihao"
function show(data) {
data.forEach(function (item, i) {
str += `<tr><td>${item.id}</td><td>${item.name}</td><td>${item.code}</td><td>${item.kind}</td></tr>`
})
tbody.innerHTML = str
}
btn.onclick = function () {
}
btn.addEventListener("click", function () {
var myAjax = new XMLHttpRequest();
myAjax.open("GET", url, true);
myAjax.onreadystatechange = function () {
if (myAjax.readyState == 4 && myAjax.status == 200) {
console.log(myAjax.response)
response = JSON.parse(myAjax.response)//将json字符串转换成json对象
// show(response.data.goods)
} else {
alert("请求失败")
return "请求失败"
}
}
myAjax.send({id: 1005, name: "张伟"})
// var res = myAjax.onreadystatechange()
})
// var myData = Mock.mock(url,'get',{
// "user|10": [{
// name: "@cname",
// "id|+1": 1000,
// img: "@img('200x100', '@cname')",
// zym: "@cparagraph(2,3)",
// city: "@city",
// b: "@Date(yyyy-MM-dd)"
// }]
// })
Mock.mock(url, "get", function (options) {
console.log(options.body.id + "-" + options.body.name);
var myId = options.body.id, myName = options.body.name
var myData = Mock.mock({
"user|100": [{
name: "@cname",
"id|+1": 1000,
img: "@img('200x100', '@cname')",
zym: "@cparagraph(2,3)",
city: "@city",
b: "@Date(yyyy-MM-dd)"
}]
})
var res = []
myData.user.forEach((item) => {
// if(item.id ==myId && item.name ==myName ){
if (item.name ==myName) {
res.push(item)
}
})
return res
})
// console.log(myData);
</script>
</body>
</html>
image.png
网友评论