Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从ajax调用时,HTML不显示在div中_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 从ajax调用时,HTML不显示在div中

Javascript 从ajax调用时,HTML不显示在div中,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,这是我正在使用的ajax脚本 mypage.php $(".sales_anchor").click(function(e) { e.preventDefault(); var store_username = $("#storeUsername").val(); var store_id = $("#storeID").val(); var sale_id = $(this).attr('id'); $.ajax( { ur

这是我正在使用的ajax脚本

mypage.php 

$(".sales_anchor").click(function(e)
{
    e.preventDefault();
    var store_username = $("#storeUsername").val(); 
    var store_id = $("#storeID").val();
    var sale_id = $(this).attr('id');
    $.ajax(
    {
        url: 'scripts/sales_sales_products.php',
        data: {"store_id": store_id, "sale_id": sale_id,"store_username": store_username},
        method: 'POST',
        success: function(data)
        {
            data = $(data).find("div#mainContentOfSale");
            $("#contents").html(data);
            console.log(data);
        }
    });
})
<div id="contents"></div>

sales\u sales\u products.php

include_once '../includes/custom_functions.php';

$stmtgetallsales = $mysqli->prepare("SELECT * FROM store_sales ss 
    INNER JOIN store s ON s.store_id=ss.store_id 
    WHERE s.store_id=?");
$stmtgetallsales->bind_param("i",$_POST['store_id']);
$stmtgetallsales->execute();
$getallsales = $stmtgetallsales->get_result();
$sales_rows = $getallsales->num_rows;
$stmtgetallsales->close();
?>
    <script src="js/3.1.1/jquery.min.js"></script>
<script src="../js/jquery.countdown.min.js"></script>
<div class="container" id="mainContentOfSale">

                <?php while($allsales = $getallsales->fetch_assoc())
                {
                  $stmtgetsaleproducts = $mysqli->prepare("SELECT * FROM store_products sp
                    INNER JOIN store_sale_products ssp ON sp.product_id = ssp.product_id 
                    WHERE ssp.sale_id=? AND sp.store_id=?");
                  $stmtgetsaleproducts->bind_param("ii",$allsales['sale_id'],$_POST['store_id']);
                  $stmtgetsaleproducts->execute();
                  $getsaleproducts = $stmtgetsaleproducts->get_result();
                  $sale_product_rows = $getsaleproducts->num_rows;
                  $stmtgetsaleproducts->close();

                ?>
                  <script>
                    $(document).ready(function() {

                      $('.products_title').hide();
                      $(".timer").each(function() {
                        var time = this.id;
                        var timeLeft = time.split('-').reverse().join('-');
                        $(this).countdown(timeLeft, function(event) {
                          $(this).text(event.strftime("%D days %H hours %M minutes %S seconds remaining"));
                        });
                      })
                    });

                  </script>
                  <div class="sale_title">
                    <h2 class="title" id="sale_products"><?php echo $allsales['sale_name']; ?></h2> <span class="badge"><?php echo $sale_product_rows; ?></span> <span class="timer" id="<?php echo $allsales['sale_till']; ?>"></span></div>

                  <div class="sale_title"><span class="discount">Discount: <span class="color-text"><?php echo $allsales['sale_discount']; ?></span>%</span></div>
                  <div class="product_area wrapper ">
                    <?php while($saleproducts = $getsaleproducts->fetch_assoc()){?>

                      <div class="product">
                        <a href="<?php echo " index.php?store=".$_POST['store_username']." &view=single&product=".$saleproducts['product_id']; ?>">
                          <div class="product-image">
                            <img src="<?php echo image_check($saleproducts['product_image1']); ?>" />
                          </div>
                        </a>
                        <div class="product-details">
                          <div class="product-name">
                            <a class="name" href="<?php echo " index.php?store=".$_POST['store_username']." &view=single&product=".$saleproducts['product_id']; ?>"><span><?php echo substr($saleproducts['product_name'], 0, 20); ?></span></a>
                          </div>
                          <div class="product-price">
                            <span class="price">@ Rs. <?php echo sale_price($allsales['sale_discount'],$saleproducts['product_price']); ?>/</span>
                          </div>
                        </div>
                      </div>

                      <?php }?>

                    <?php } ?>
当我用sales_anchor类调用锚定标记时,它会调用这个脚本,在inspect元素和innetwork选项卡中,当我检查脚本时,它会填充数据和html,一切正常。但是它没有在当前页面和目录部分被调用。我做错了什么


在屏幕截图中遇到JSON.parse错误。请注意,结果窗口上方的红色条在JSON数据的第1行第9列显示SyntaxError:JSON.parse:unexpected字符

这是因为$.ajax试图将响应作为JSON字符串解析为默认行为的对象

请将ajax函数更改为:

$(".sales_anchor").click(function(e)
{
    e.preventDefault();
    var store_username = $("#storeUsername").val(); 
    var store_id = $("#storeID").val();
    var sale_id = $(this).attr('id');
    $.ajax(
    {
        url: 'scripts/sales_sales_products.php',
        dataType: "text",
        data: {"store_id": store_id, "sale_id": sale_id,"store_username": store_username},
        method: 'POST',
        success: function(data)
        {
            data = $(data).find("div#mainContentOfSale");
            $("#contents").html(data);
            console.log(data);
        }
    });
})
<div id="contents"></div>

在屏幕截图中遇到JSON.parse错误。请注意,结果窗口上方的红色条在JSON数据的第1行第9列显示SyntaxError:JSON.parse:unexpected字符

这是因为$.ajax试图将响应作为JSON字符串解析为默认行为的对象

请将ajax函数更改为:

$(".sales_anchor").click(function(e)
{
    e.preventDefault();
    var store_username = $("#storeUsername").val(); 
    var store_id = $("#storeID").val();
    var sale_id = $(this).attr('id');
    $.ajax(
    {
        url: 'scripts/sales_sales_products.php',
        dataType: "text",
        data: {"store_id": store_id, "sale_id": sale_id,"store_username": store_username},
        method: 'POST',
        success: function(data)
        {
            data = $(data).find("div#mainContentOfSale");
            $("#contents").html(data);
            console.log(data);
        }
    });
})
<div id="contents"></div>

html代码中的contents div在哪里?在另一个脚本中。jquery代码在另一个脚本中console.log工作正常吗?不!但是network标记显示了在调用完成后控制台中遇到错误的htmlDo?在html代码中contents div在哪里?在另一个脚本中。jquery代码在另一个脚本中console.log工作正常吗?不!但是网络标签显示了htmlDo,在呼叫完成后,您在控制台中遇到错误了吗?好的,这样就行了!我还删除了data=$data.finddivmainContentOfSale;这条线成功了。但它并没有显示完整的数据。您可以在屏幕截图中看到一个product_area div,该div没有显示在mypage.php中,而是显示在console中!我还删除了data=$data.finddivmainContentOfSale;这条线成功了。但它并没有显示完整的数据。您可以在屏幕截图中看到一个product_area div,该div没有显示在mypage.php中,而是显示在console中。