简体   繁体   English

Ajax 从 JS 到 PHP 的 POST 请求无法正常工作

[英]Ajax POST request from JS to PHP does not work correctly

The idea of the code I'm trying to run is this: When a $ _GET [] request arrives on the products.php page, I pass it via an ajax POST request to the get_data_products.php page and get data from there. The idea of the code I'm trying to run is this: When a $ _GET [] request arrives on the products.php page, I pass it via an ajax POST request to the get_data_products.php page and get data from there.

PHP PHP

 if(isset($_GET['cat'])){
   $filter_cat = $_GET['cat'];
   
   echo '<input type="hidden" value="'.$filter_cat.'" class="getcat" />';
 }

JS JS

 function filter_data2() {
     $('.filter_data').html('<div id="loading" style="" ></div>');
     var action = 'fetch_data';
     var filter = [];
     var category = $('.getcat').val();
     filter.push(category);

       $.ajax({
           url:"<?php echo $g['url']; ?>get_date_products.php",
           method:"POST",
           data:{action:action,category:filter},
           success:function(data){
               $('.filter_data').html(data);
           }
       });
 }

The problem is: When I get for example $_GET['category'] = 2 and ajax sends the data to get_data_products.php and then selects another category and $_GET['category'] = 12 sometimes it is not sent.问题是:当我得到例如$_GET['category'] = 2和 ajax 将数据发送到get_data_products.php然后选择另一个类别和$_GET['category'] = 12有时它不会发送。 When I give var_dump ( $_GET['category'] ) it always contains the correct value, but for some reason ajax does not send it.当我给var_dump ( $_GET['category'] ) 它总是包含正确的值,但由于某种原因 ajax 不发送它。

I can see a typo here url:"get_ date _products.php" doesn't it supposed to be data?我可以在这里看到一个错字 url:"get_ date _products.php" 它不应该是数据吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM