php - 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!";
?>
解决方案
推荐阅读
- python - pyhamcrest - 比较两个列表
- graphql - 如何在其他文件中使用graphql类型进行关系而不重复
- qt - QML:原始模型中的动态视图重新排序
- android - Xamarin UITest ScrollDownTo for Android 真的很慢
- javascript - 如何在 CSS 规则中选择具有 Windows 格式路径的属性?
- node.js - 如何使用 Node.js 和 Axios 将文件上传到 AWS 中的预签名 URL?
- mysql - 如何在 Mariadb 的 regexp_replace 中使用正则表达式标志?
- javascript - 如何从firestore数据库返回值?
- c++ - 静态成员声明为 const 但初始化为 constexpr
- arrays - 带有姓名、姓氏等的 Java 数组