dqx36753 2016-06-11 15:18
浏览 123

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.

  • 写回答

1条回答

  • duannao3402 2016-06-11 15:54
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog