jQuery,JavaScript - 网络错误?

I am working on building a Javascript/jQuery and AJAX website. However, while testing I am encountering the following error:

Timestamp: 6/11/2016 10:13:45 AM
Error: NetworkError: A network error occurred.

To me, the most obvious culprit would be the AJAX call made in my script; however, I tried commenting it out and still received the same error. When the website is first loaded it displays three alert boxes (selection=category&detail=test, and so on), but then the error appears and changing the selection does not trigger the alert.

Here is my main page:

<?php
include('header.php');
?>

<div id='mainContent'>

<?php /* if(!$_SESSION['admin']) {
echo "<p>You do not have permission to view this page!</p>";
} else { */
echo "<form method='post' action='addItem.php'> 

<div class='form-group'>
<label>Category</label>
<select id='categorySelection' name='categorySelection'>
<option value=1>Playstation</option>
<option value=2>Wii</option>
<option value=3>Gamecube</option>
<option value=4>N64</option>
<option value=5>Other Console</option>
<option value=6>DS</option>
<option value=7>Game Boy</option>
<option value=8>Other Handheld</option>
<option value=9>DVD</option>
</select>
</div>

<div class='form-group'>
<label>Item</label>
<select id='itemSelection' name='itemSelection'>
</select>
</div>

<div class='form-group'>
<label>Condition:</label>
<select id='conditionSelection' name='conditionSelection'>
<option value=1>Acceptable</option>
<option value=2>Good</option>
<option value=3>Very Good</option>
<option value=4>New</option>
</select>
</div>

<div class='form-group'>
<label>Price: </label>
<input id='itemPrice' type='text' name='itemPrice' />
</div>

<div class='form-group'>
<label>Description: </label>
<textarea id='itemDescription' name='itemDescription'></textarea>
</div>
</form>";
 // }
?>

</div>
<script>

function selectionHandler(selectedAction, selectedValue) {
var gameData = "selection=" + selectedAction + "&detail=" + selectedValue;

alert(gameData);


 $.ajax({
type: 'POST',
url: 'filter.php',
data: gameData,
success: function(returnData) {

if(selectedAction == 'category') {
$('#itemSelection').html(returnData);
} if(selectedAction == 'game') {
$('#itemPrice').val(returnData)
} else {
$('itemDescription').val(returnData);
} 

} // end return
});   // end ajax 
} // end handler

$(document).ready(function() {
$("#categorySelection").on("change", selectionHandler('category', "test" ) ) ;
$("#itemSelection").on('change', selectionHandler('game',  "test" ) );
$("#conditionSelection").on('change', selectionHandler('condition',  "test" ) );

}); // end ready
</script>
<?php

include('footer.php');
?>

and my PHP

<?php
include("header.php");
$db = new pdoAccess("localhost","root","");

$selection = $_POST['selection']; // either game, category, or condition

$detail = $_POST['detail'] // either categoryID or ISBN/Item ID
if($selection == 'category') {
$products = $db->filterByCategory($detail);
$html = "";
foreach($products as $product) {
$html += "<option value={$product->upc)}>$product->title</option>";
}
return $html;
} elseif ($selection = 'game') {
return $db->getProductPrice($detail);
} else {
return $db->getCategoryDescription($detail);
}

?>

Thanks!

EDIT: It should be noted that I also tried other events such as focusout and select. Same issue.

duan97689
duan97689 我建议将你的ajax代码移到你的文档就绪块中。它有可能尝试使用尚未加载的东西。如果将它移动到一个函数中会更好,然后在出错时你可以再次调用该函数。因此,例如,如果有人丢失了互联网连接,它将继续尝试,直到他们再次建立连接。除此之外,我不知道。您可以检查状态变量是否等于成功,然后执行您的代码,如果不是console.log则要测试。
4 年多之前 回复
dsuhx86802
dsuhx86802 谢谢,但我很确定。该页面唯一的唯一脚本是'filter.php',我将其与整个AJAX请求一起注释掉。网站的其余部分工作正常,如果我的PDO连接出现问题,肯定会失败。奇怪的是,我确实在其他页面上得到了这个错误-我从来没有注意到它,因为它不会影响功能。有可能CDN链接(到jQuery)可能是问题吗?再次感谢。
4 年多之前 回复
dotxo86440
dotxo86440 你确定错误不是来自你的PHP吗?像pdo数据库连接?
4 年多之前 回复

1个回答

In php string cocatinating is by dot . and use quotes also change here like this

 $html .= "<option value='{$product->upc)}'>{$product->title}</option>";

Also use double == to compare here

 } else if ($selection == 'game') {

Use echo in ajax instead of return

duanmao1319
duanmao1319 你应该从函数外部调用selectionHandler
4 年多之前 回复
douhei2002
douhei2002 在ajax中使用echo而不是return
4 年多之前 回复
doumao6048
doumao6048 从哪里调用selectionHandler
4 年多之前 回复
douxiong2001
douxiong2001 感谢您指出(我还没有调试PHP); 但是它没有解决问题。 我为PHP指定了完整,错误和成功函数。 完成和成功函数都会在第一次(页面加载)时触发,但在此之后,它似乎就像JavaScript“中断”一样。 更改选择甚至不会产生第一个警报消息。 再次感谢。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐