gpt4 book ai didi

javascript - JS ProgressBar 从 AJAX 调用的 PHP While 循环内部更新?

转载 作者:行者123 更新时间:2023-11-28 00:47:49 25 4
gpt4 key购买 nike

我有一个带有表单的 PHP 页面。提交表单后,它会通过 AJAX 调用另一个 PHP 页面来调用 MySQL,然后处理结果,并将其返回到第一个 PHP 页面。 MySQL 处理发生在 while 循环内。我想更新一个指示循环进度的进度条。但我得到:

parsererror
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

什么也没发生。如果我下面做的事情是错误的,有什么想法吗?如何通过名为 while 循环的 AJAX 更新进度条?

以下是粗略的代码:

PHP 主页面:

<html>
<head>
<link rel="stylesheet" type="text/css" href="jquery-ui.css">
<script type='text/javascript' src='jquery-1.11.1.min.js'></script>
<script type='text/javascript' src='jquery-ui-1.10.4.min.js'></script>
<script type='text/javascript' src='my.js'></script>
</head>

<body onLoad="onLoad()">

<form action="" method="POST" id="myForm" name="myForm">
<div id="progressBar"></div>
<input class="txt"
id="btnSubmit"
style="margin-top: 12pt; font-family: arial; color: grey; font-weight: bold; font-size: 15pt;"
type="submit"
name="action"
value="SEARCH" />
</form>
</body>
</html>

my.js 有:

function onLoad() {
$('#progressBar').progressbar({ disabled: true });
$('#progressBar').hide();
}

$(document).ready(function() {
$("#myForm").submit(function(event) {
$(function () {
.ajax({
url: 'myCall.php', // the script to call to get data
type: "POST",
data: { data: 'something'}
},
dataType: 'json',
success: function(data) {
// do something
},
error: function (jqXHR, textStatus, errorThrown){
console.log(textStatus, errorThrown);
},


});


});
});

myCall.php对MySQL数据库进行了一些调用,然后进行后期处理:

$result = mysqli_query($mysqli, $sql); 

$j = 1;
// enable and show the bar
echo '<script language="javascript"></script>';
echo '$("#progressBar").show();';
echo '$("#progressBar").progressbar({';
echo 'disabled: false,';
echo 'value: '.$j.',';
echo 'max: '.$result->num_rows.',';
echo '});';
echo '</script>';

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
// doing stuff and then update the bar
// update
echo '<script language="javascript"></script>';
echo '$("#progressBar").progressbar({';
echo 'value: '.$j.',';
echo '});';
echo '</script>';
}
// disable and hide the bar
echo '<script language="javascript"></script>';
echo '$("#progressBar").progressbar({';
echo 'disabled: true,';
echo '});';
echo '$("#progressBar").hide();';
echo '</script>';

最佳答案

您正在解析的 JSON 似乎不是有效的 JSON。我会打印出您尝试通过解析器运行的 JSON,并通过 JSON 验证器运行它,例如 http://jsonformatter.curiousconcept.com/ .

另外,下面的代码对我来说看起来不干净,这可能会导致问题。我不确定您是否使用更标准化的 JSON 解析器。如果是这样,它可能不会期望数据对象中存在数据对象。这是一个完整的猜测,但你可能应该改变

         .ajax({
url: 'myCall.php', // the script to call to get data
type: "POST",
data: { data: 'something'}
},

         .ajax({
url: 'myCall.php', // the script to call to get data
type: "POST",
data: { "key1" : "value1"}
},

我认为您实际上并没有显示问题中 JSON 的解析位置。你能准确地展示你如何解析它以及你正在解析什么吗?

谢谢!

关于javascript - JS ProgressBar 从 AJAX 调用的 PHP While 循环内部更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27152206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com