这是一段jquery代码,请帮我修改为,如果五秒内链接不到服务器则打印"未找到服务器": $.post("http://localhost:8080/xxxx/home.do",temp, function(data) { let com = document.getElementById("commodity_row"); let card = ``; $.each(data, function(i, v) { card += ` <div class="col-xl-2 col-lg-3 col-md-3 col-sm-4 col-6 mb-2"> <div class="card card_aive cur-poi card_color"> <img src="http://localhost:8080/xxxx/${v.g_Pic}" class="card-img-top " > <div class="card-body"> <span class="card-text">${v.g_Name}</span> <p class="card-text font-size-12 color-red">${v.gCategories_Id.c_Name}</p> <p id="price" class="card-text text-right color-red">¥${v.g_Money}</p> </div> </div> </div>`; }) com.innerHTML = card; }, "json");
时间: 2024-03-07 15:50:52 浏览: 120
Jquery中$.post和$.ajax的用法小结
5星 · 资源好评率100%
你可以使用setTimeout函数来实现五秒钟后未链接成功则打印“未找到服务器”的效果,代码如下:
```javascript
$.ajax({
url: "http://localhost:8080/xxxx/home.do",
type: "POST",
data: temp,
dataType: "json",
success: function(data) {
let com = document.getElementById("commodity_row");
let card = ``;
$.each(data, function(i, v) {
card += ` <div class="col-xl-2 col-lg-3 col-md-3 col-sm-4 col-6 mb-2">
<div class="card card_aive cur-poi card_color">
<img src="http://localhost:8080/xxxx/${v.g_Pic}" class="card-img-top " >
<div class="card-body">
<span class="card-text">${v.g_Name}</span>
<p class="card-text font-size-12 color-red">${v.gCategories_Id.c_Name}</p>
<p id="price" class="card-text text-right color-red">¥${v.g_Money}</p>
</div>
</div>
</div>`;
});
com.innerHTML = card;
},
error: function(jqXHR, textStatus, errorThrown) {
setTimeout(function() {
console.log("未找到服务器");
}, 5000);
}
});
```
这里使用了$.ajax()函数来发送请求,并在请求失败时使用setTimeout函数来实现五秒钟后打印“未找到服务器”的效果。
阅读全文