首页 > 解决方案 > ajax 调用没有得到任何响应

问题描述

我在 wordpress 插件上有三个下拉列表,第二个值取决于第一个下拉列表的选择值,所以我使用 ajax 发送请求但没有得到任何响应这是我的代码,我在坐下并尝试使用它

索引.php

  <?php
/**
 * Plugin Name:index
 * 
 * Version: 1.0
 *
 */


 function try_form()
    {



 include "connect_db.php"; ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<table>
  <tr>
    <td>Country</td>
    <td>
       <!-- Country dropdown -->
       <select id='sel_country' >
          <option value='0' >Select Country</option>
          <?php 
          ## Fetch countries
          $stmt = $conn->prepare("SELECT * FROM cities ");
          $stmt->execute();
          $countriesList = $stmt->fetchAll();

          foreach($countriesList as $country){
             echo "<option value='".$country['ID']."'>".$country['city_name']."</option>";
          }
          ?>
       </select>
    </td>
  </tr>

  <tr>
    <td>State</td>
    <td>
      <select id='sel_state' >
         <option value='0' >Select State</option>
      </select>
    </td>
  </tr>
   <tr>
    <td>City</td>
    <td>
      <select id='sel_city' >
        <option value='0' >Select City</option>
      </select>
    </td>
  </tr>
</table>
  </table>
   <script type="text/javascript">    
  
  
  
  
  
  $(document).ready(function(){

  // Country
  $('#sel_country').change(function(){

     var countryid = $(this).val();

     // Empty state and city dropdown
     $('#sel_state').find('option').not(':first').remove();
     $('#sel_city').find('option').not(':first').remove();

     // AJAX request
     $.ajax({
       url: "ajaxfile.php",
       type: 'post',
       data: {request: 1, countryid:countryid},
       dataType: 'json',
       success: function(response){
alert (countryid);
         var len = response.length;

         for( var i = 0; i<len; i++){
           var ID = response[i]['ID'];
           var name = response[i]['name'];

           $("#sel_state").append("<option value='"+ID+"'>"+name+"</option>");

         }
       },
        error: function(){
            alert('failure');}
     });

  });

  // State
  $('#sel_state').change(function(){
     var stateid = $(this).val();

     // Empty city dropdown
     $('#sel_city').find('option').not(':first').remove();

     // AJAX request
     $.ajax({
       url: 'ajaxfile.php',
       type: 'post',
       data: {request: 2, stateid: stateid},
       dataType: 'json',
       success: function(response){

         var len = response.length;

         for( var i = 0; i<len; i++){
           var id = response[i]['id'];
           var name = response[i]['name'];

           $("#sel_city").append("<option value='"+id+"'>"+name+"</option>");

         }
       }
     });
   });
});
  </script>
<?php  } add_shortcode('t', 'try_form');

ajaxfile.php 文件

<?php

include "connect_db.php";

$request = 0;

if(isset($_POST['request'])){
   $request = $_POST['request'];
}

// Fetch state list by countryid
if($request == 1){
   $countryid = $_POST['countryid'];

   $stmt = $conn->prepare("SELECT * FROM m where mm= :country");
  // $stmt->bindValue(':country', (int)$countryid, PDO::PARAM_INT);

   $stmt->execute();
   $statesList = $stmt->fetchAll();

   $response = array();
   foreach($statesList as $state){
      $response[] = array(
        "ID" => $state['ID'],
        "name" => $state['name']
      );
   }

   echo json_encode($response);
   exit;
}

// Fetch city list by stateid
if($request == 2){
   $stateid = $_POST['stateid'];

   $stmt = $conn->prepare("SELECT * FROM cities WHERE state=:state ORDER BY name");
   $stmt->bindValue(':state', (int)$stateid, PDO::PARAM_INT);

   $stmt->execute();
   $statesList = $stmt->fetchAll();

   $response = array();
   foreach($statesList as $state){
      $response[] = array(
         "id" => $state['id'],
         "name" => $state['name']
      );
   }

   echo json_encode($response);
   exit;
}

所以知道为什么我没有得到任何回应

编辑我已经看到了我的浏览器开发者工具栏的网络标签,我明白了

[ https://i.stack.imgur.com/Y59jC.png ] 我将 bindValue 编辑为 bindParam,现在我遇到了失败

标签: phpajaxwordpress

解决方案


推荐阅读