由于某种原因,以下ajax成功功能无法正常工作
我想做什么?
AJAX致电:
$('#datatable').on( 'click', '.recipeFlavours', function (e) {
var token = '<?php echo json_encode($token); ?>';
var flavourList = true;
var table = $('#datatable').DataTable();
var rowSelector;
var li = $(this).closest('li');
if ( li.length ) {
rowSelector = table.cell( li ).index().row;
}
else {
rowSelector = $(this).closest('tr');
}
var recipeID = table.row(rowSelector).data().recipe_id;
$.ajax({
type: "POST",
url: "controllers/recipeControl.php",
data: { token: token, recipeID: recipeID, flavourList: flavourList },
success: function(data){
$(".success_container").html(data);
console.log(data);
var popup = '<div class="modal fade" id="notes-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;"><div class="modal-dialog"><div class="notes-modal-container"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h1></h1><h3>Recipe Flavours</h3><br>' + data + '</div></div></div>';
$(popup).modal('toggle');
},
});
return false;
});
控制器:在没有PHP
json_encode()
/**
GET RECIPE FLAVOURS FOR POPUP
*/
if(isset($_POST['flavourList'])) {
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && isset($_POST['token']) && json_decode($_POST['token']) === $_SESSION['token']){
//echo json_encode($Recipes->getRecipeFlavours($recipeid));
$html = "<div>";
$content = $Recipes->getRecipeFlavours($_POST['recipeID']);
foreach($content as $value) {
$html .= $value['recipe_flavour_name'].$value['recipe_flavour_percent'];
}
$html .= "</div>";
echo json_encode($html);
}
}
获取风味和百分比的功能:
/**
GET RECIPE FLAVOUR LIST
*/
public function getRecipeFlavours($recipeid) {
$query = 'SELECT recipe_flavour_name, recipe_flavour_percent FROM recipe_flavours WHERE recipe_flavour_recipe_id = :recipeid';
$stmt = $this->queryIt($query);
$stmt = $this->bind(':recipeid',$recipeid);
if($this->execute()) {
return $this->resultset();
}
}
作为PHP,这是输出:
作为AJAX中的成功数据:
答案 0 :(得分:1)
删除json_encode(),因为您使用HTML作为ajax的数据类型
if(isset($_POST['flavourList'])) {
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && isset($_POST['token']) && json_decode($_POST['token']) === $_SESSION['token']){
//echo json_encode($Recipes->getRecipeFlavours($recipeid));
$html = "<div>";
$content = $Recipes->getRecipeFlavours($_POST['recipeID']);
foreach($content as $value) {
$html .= $value['recipe_flavour_name'].$value['recipe_flavour_percent'];
}
$html .= "</div>";
echo $html;
}
}
答案 1 :(得分:1)
您在控制器中返回响应时使用json_encode,但在javascript中处理响应时不进行解码。
所以请删除json_encode并尝试。它会起作用。
答案 2 :(得分:0)
试试这段代码:
var years = moment().diff('1988-01-30', 'years');