在ajax错误上显示错误消息:function()

时间:2017-05-20 07:35:29

标签: javascript php jquery

  

如果我的ajax中出现错误部分,我想在定向页面中显示错误消息。但是当它转到此定向页面时,如果我刷新此页面则显示错误消息,它不会显示错误消息。它是我的代码。< / p>      

的index.html

<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>
  

tracking.php

<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>

1 个答案:

答案 0 :(得分:0)

尝试将您的脚本代码放在HTML代码下面。因为Jquery无法找到选择器&#34; #error&#34;在html加载之前。 试一试。