首页 > 解决方案 > Ajax 返回状态码 0 但脚本仍在工作

问题描述

我有一个名为 data.php 的脚本,我从 ajax 调用它来刷新数据库。当我们将该脚本称为其空数据库表并在 while 循环中添加新记录时。它工作正常,但是当我从 ajax 调用时。ajax 在插入一些记录后给我错误 0,但是我的脚本继续并按预期插入所有记录,但是由于我的 ajax 停止工作,我没有得到成功调用。

<script type="text/javascript">
$(document).ready(function(){
    $(document).on('click', '#button1', function(e){
        jQuery("#musicinfo1").toggle();
        $.ajax({
            type: 'POST',
            url: 'data.php',
            //timeout: 300000,
            error: function(xhr, ajaxOptions, thrownError){
                alert(xhr.status);
            },
            success: function(data) {
              jQuery("#musicinfo1").toggle("slow");
              jQuery("#musicinfo").toggle("slow");
              
              //setTimeout('moomoo()', 1000);
          
            }
        });
    });
});

function moomoo() {
   location.reload();
}
</script> 

这是我的data.php

<?php
error_reporting(0);
include("includes/connection.php");
require 'scraping.php';
$quotes_qry="SELECT * FROM tbl_url ORDER BY id ASC";
 
$result=mysqli_query($mysqli,$quotes_qry);

$q = "TRUNCATE TABLE `tbl_data`";
$resultnew=mysqli_query($mysqli,$q);

$i=0;
while($row=mysqli_fetch_array($result)){
    $html = file_get_html($row['url']);
    $$html = file_get_html($row['url']);
    $url = $row['url'];
    $name= $html->find('h1[class=page-title]', 0)->plaintext;
    $one_year_return= $html->find('div[class=company-highlights--right-close]', 0)->plaintext;
    $property_type= $html->find('div[class=company-highlights--data-content]', 0)->plaintext;
    $company_type= $html->find('div[class=company-highlights--data-content]', 1)->plaintext;
    $exchange= $html->find('div[class=company-highlights--data-content]', 2)->plaintext;
    $listing_status= $html->find('div[class=company-highlights--data-content]', 3)->plaintext;
    $ticker= $html->find('div[class=company-highlights--data-content]', 4)->plaintext;
    $previous_closed= $html->find('.field--name-field-stock-price-prev-close .field__item', false)->getAttribute('content');
    $current_high= $html->find('.field--name-field-stock-price-current-high .field__item', false)->getAttribute('content');
    $current_low= $html->find('.field--name-field-stock-price-current-low .field__item', false)->getAttribute('content');
    
    $thirty_days_volume= $html->find('.field--name-field-30-days-average-volume .field__item', false)->getAttribute('content');
    
    $fifty_two_week_high= $html->find('.field--name-field-stock-52-week-high .field__item', false)->getAttribute('content');
    $fifty_two_week_low= $html->find('.field--name-field-stock-52-week-low .field__item', false)->getAttribute('content');
    
    $dividend_yield= $html->find('.field--name-field-stock-dividend-yield .field__item', false)->getAttribute('content');
    $previous_quarter= $html->find('.field--name-field-stock-funds-from-operation .field__item', false)->getAttribute('content');
    
    $count0 = count($html->find('div[field--name-field-stock-last-paid-div-value]', 0));
    if($count0 > 0){
        $last_paid_value= $html->find('.field--name-field-stock-last-paid-div-value .field__item', false)->getAttribute('content');
    }else{
        $last_paid_value = 0;
    }
    $days_volume = 0;
    $market_cap=0;
    
    $qry = "INSERT INTO tbl_data            
                        (`url`,`name`,`one_year_return`,`property_type`,
                        `company_type`,`exchange`,`listing_status`,
                        `ticker`, `previous_closed`,`current_high`,
                        `current_low`, `thirty_days_volume`,
                        `fifty_two_week_high`,`fifty_two_week_low`,
                        `last_paid_value`,`dividend_yield`,
                        `previous_quarter`,`days_volume`, `market_cap`) 
                VALUES ('$url','$name','$one_year_return',
                        '$property_type','$company_type','$exchange',
                        '$listing_status', '$ticker', '$previous_closed', 
                        '$current_high', '$current_low',
                        '$thirty_days_volume', '$fifty_two_week_high', 
                        '$fifty_two_week_low','$last_paid_value',
                        '$dividend_yield','$previous_quarter',
                        '$days_volume','$market_cap')";
    
    $result2= mysqli_query($mysqli,$qry);
    $i++;
}
echo "Data Refresh Done!";
?>

标签: phpajax

解决方案


推荐阅读