Javascript 要在ajax错误上显示错误消息,请执行以下操作:function()
如果ajax中出现错误部分,我想在定向页面中显示错误消息。但当它转到此定向页面时,如果我刷新此页面,则不会显示错误消息。下面是我的代码 index.htmlJavascript 要在ajax错误上显示错误消息,请执行以下操作:function(),javascript,php,jquery,Javascript,Php,Jquery,如果ajax中出现错误部分,我想在定向页面中显示错误消息。但当它转到此定向页面时,如果我刷新此页面,则不会显示错误消息。下面是我的代码 index.html tracking.php $(文档).ready(函数(){ var from=“”; $(“#加载”).show(); $.ajax({ 键入:“获取”, 网址:'http://apis.abc.abc/api/Get_Loadsheet_Details/', 数据类型:“json”, 成功:功能(响应){ $(“#加载”).hid
tracking.php
$(文档).ready(函数(){
var from=“”;
$(“#加载”).show();
$.ajax({
键入:“获取”,
网址:'http://apis.abc.abc/api/Get_Loadsheet_Details/',
数据类型:“json”,
成功:功能(响应){
$(“#加载”).hide();
控制台日志(响应);
document.getElementById('lrid')。innerHTML=“LR编号:”+响应[0]。LRSUFIX+响应[0]。LR\U编号;
document.getElementById('委托')。innerHTML=响应[0]。公司名称;
document.getElementById('from')。innerHTML=响应[0]。LOADFROMMST;
document.getElementById('dest')。innerHTML=响应[0]。目标;
document.getElementById('case')。innerHTML=响应[0]。没有\u的\u PKT;
document.getElementById('lrsta')。innerHTML=响应[0]。LR\u状态;
document.getElementById('lr')。innerHTML=response[0]。lrLoadStatus;
document.getElementById('vecno')。innerHTML=响应[0]。车辆号;
document.getElementById('lrstatus')。innerHTML=响应[0]。加载状态;
document.getElementById('ldate')。innerHTML=响应[0]。日期;
},错误:函数(错误){
console.log(错误);//警报('hi');
$(“#加载”).hide();
$('#error').html(“在此LR编号上未找到任何数据”);
}
});
});
跟踪详细信息
订单信息
收货人
从…起
目的地
案例
LR状态
LR
负载信息
车辆号码:
负载状态
装货日期:
尝试将脚本代码放在HTML代码下面。因为Jquery在加载html之前找不到选择器“#error”。
试一试。找不到任何id错误的dom我不知道ajax在这里有什么用途。您可以直接使用php中的file_get_contents()函数从API url获取值
<form method="post" name="myForm" action="tracking.php">
<input type="text" name="number" id="number" placeholder="Enter LR Number" required>
<input type="submit" name="submit" value="Go">
</form>
<script>
$(document).ready(function () {
var from = "";
$('#loadings').show();
$.ajax({
type: "GET",
url: 'http://apis.abc.abc/api/Get_Loadsheet_Details/<?php echo $number; ?>',
dataType: 'json',
success: function (response) {
$('#loadings').hide();
console.log(response);
document.getElementById('lrid').innerHTML = "LR NO: " + response[0].LRSUFIX + response[0].LR_NO;
document.getElementById('consign').innerHTML = response[0].COMPANY_NAME;
document.getElementById('from').innerHTML = response[0].LOADFROMMST;
document.getElementById('dest').innerHTML = response[0].DESTINATION;
document.getElementById('case').innerHTML = response[0].NO_OF_PKT;
document.getElementById('lrsta').innerHTML = response[0].LR_STATUS;
document.getElementById('lr').innerHTML = response[0].lrLoadStatus;
document.getElementById('vecno').innerHTML = response[0].VEHICLE_NO;
document.getElementById('lrstatus').innerHTML = response[0].LOADIG_STATUS;
document.getElementById('ldate').innerHTML = response[0].DATE;
}, error: function (errors) {
console.log(errors);//alert('hi');
$('#loadings').hide();
$('#error').html("<h2><span style='color:red;'>No data found on this LR No.</span></h2>");
}
});
});
</script>
<section>
<div id="error"></div>
<div class="loader-div" style="position:relative;" ><img id="loadings" src="images/loading2.gif" style=" left: 40%;
position: absolute;
top:250px;
z-index:1111;"></div>
<div class="container" >
<div class="body_left" id="container">
<h1 class="heading_3">Tracking Details</h1>
<table width="100%" class="track">
<tr>
<th >Order Information</th>
<th id="lrid"></th>
</tr>
<tr>
<td>Consignee</td>
<td id="consign"> </td>
</tr>
<tr>
<td>From</td>
<td id="from"></td>
</tr>
<tr>
<td>Destination</td>
<td id="dest"> </td>
</tr>
<tr>
<td>Cases</td>
<td id="case"> </td>
</tr>
<tr>
<td>LR Status</td>
<td id="lrsta"></td>
</tr>
<tr>
<td>LR</td>
<td id="lr"></td>
</tr>
</table>
<br>
<br>
<table width="100%" class="track">
<tr>
<th colspan="2">Load Information</th>
</tr>
<tr>
<td>VEHICLE NUMBER: </td>
<td id="vecno"></td>
</tr>
<tr>
<td>LOAD STATUS </td>
<td id="lrstatus"> </td>
</tr>
<tr>
<td>LOAD DATE:</td>
<td id="ldate"> </td>
</tr>
</table>
</div>
</div>
</section>