对PHP变量的 AJAX 请求不按预期工作

pokxtpni  于 2023-06-20  发布在  PHP
关注(0)|答案(2)|浏览(107)

我正在发出 AJAX GET请求,但收到的响应不符合预期。响应通过,但PHP变量为空。下面是我的jQuery:

jQuery(document).ready(function($) {
   $.ajax({
      url: '/wp-admin/admin-ajax.php',
      method: 'GET',
      data: {
         action: 'geoip_detect2_get_info_from_current_ip',
      },
      success: function(response) {
         var country = response.country.names.en;
         var isoCode = response.country.iso_code;
  
         console.log('Country: ' + country);
         console.log('ISO Code: ' + isoCode);  
      },
      error: function(xhr, status, error) {
         // Handle any errors that occur during the AJAX request
         console.error(error);
      }
   });    
});

更新的 AJAX 请求的答案

$.ajax({
      url: '/wp-admin/admin-ajax.php',
      method: 'GET',
      data: {
        action: 'geoip_detect2_get_info_from_current_ip',
      },
      success: function(response) {
        var country = response.country.names.en;
        var isoCode = response.country.iso_code;
  
        console.log('Country: ' + country);
        console.log('ISO Code: ' + isoCode);
  
        // Store the values in hidden input fields
        $('#hidden-country').val(country);
        $('#hidden-isoCode').val(isoCode);
      },
      complete: function() {
        // Retrieve the values from hidden input fields
        var country = $('#hidden-country').val();
        var isoCode = $('#hidden-isoCode').val();
  
        // Make an AJAX request to your PHP script in the theme
        $.ajax({
          url: '/wp-content/themes/bethub/inc/product_layout/side_block.php',
          method: 'POST',
          data: {
            country: country,
            isoCode: isoCode
          },
          success: function(response) {
            console.log(response);
            // Handle the response from your PHP script
          },
          error: function(xhr, status, error) {
            console.error(error);
          }
        });
      },
      error: function(xhr, status, error) {
        // Handle any errors that occur during the AJAX request
        console.error(error);
      }
    });
  });

在控制台中,正如预期的那样,我得到了正确的结果,没有错误:

Country: Ukraine
ISO Code: UA
XHR finished loading: GET "https://mywebsite.com/wp-admin/admin-ajax.php?action=geoip_detect2_get_info_from_current_ip"
XHR finished loading: POST "https://mywebsite.com/wp-content/themes/theme_name/inc/product_layout/side_block.php".

然而,当我想在WordPress主题的PHP文件中输出结果时,我得到一个空结果:

$country = $_POST['country'];
$isoCode = $_POST['isoCode'];
echo "Country: " . $country . "<br>";
echo "ISO Code: " . $isoCode;

我做错了什么?
我使用的插件是geoip-detect
服务器:Nginx php-fpm
Nginx日志中没有错误。

cnjp1d6j

cnjp1d6j1#

AJAX 请求是异步的,这意味着第二个请求可能在第一个请求完成之前就发出了,所以在第一个请求成功后,尝试在complete function中发出第二个请求,如下所示。

$.ajax({
  url: '/wp-admin/admin-ajax.php',
  method: 'GET',
  data: {
    action: 'geoip_detect2_get_info_from_current_ip',
  },
  success: function(response) {
    var country = response.country.names.en;
    var isoCode = response.country.iso_code;

    console.log('Country: ' + country);
    console.log('ISO Code: ' + isoCode);
    $('#hiddenCountry').val(country);
    $('#hiddenIsoCode').val(isoCode);
  },
  complete: function() {
    // Make an AJAX request to your PHP script in the theme
    $.ajax({
      url: '/wp-content/themes/my_theme/inc/product_layout/side_block.php',
      method: 'POST',
      data: {
        country: $('#hiddenCountry').val(),
        isoCode: $('#hiddenIsoCode').val()
      },
      success: function(response) {
        console.log(response);
        // Handle the response from your PHP script
      },
      error: function(xhr, status, error) {
        console.error(error);
      }
    });
  },
  error: function(xhr, status, error) {
    // Handle any errors that occur during the AJAX request
    console.error(error);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<input type="hidden" id="hiddenCountry">
<input type="hidden" id="hiddenIsoCode">
2izufjch

2izufjch2#

正如上面的评论中提到的,PHP是在服务器上,而不是客户端。$_GET处理从表单中提取传递到php文件的URL参数。
因为你是在php中手动调用这个库,所以你不是在客户端。您使用的函数(fetch action)不会生成相同的响应。它将使用发出请求的服务器的IP。
这意味着您应该使用geoip_detect2_get_info_from_ip(),并输入ip地址和其他必需的参数。
有关库的API以及函数中的参数的信息,请访问repo:link to repo

相关问题