【问题标题】:POST array is empty after the data sent through ajax通过ajax发送数据后POST数组为空
【发布时间】:2016-08-02 11:13:50
【问题描述】:

我再次在我的代码中使用 ajax。我正在为我的项目制作实时搜索 ajax 系统,但是当我将数据发送到 search.php 和 var_dump POST 数组时,我得到了空的 post 数组。

AJAX

function searching(string){
 $.ajax({
  url  : "request/search.php",
  type : "POST",
  data : "search="+string,
  type : "text",
  beforeSend : function(http){

  },
  success : function(response,status,http){
    alert(response);
  },
   error : function(http,status,error){
        $('.response').html("<span class='error'>Something went wrong</span>");
        $(".response").slideDown();
    }
   })
 }

HTML

<form id="searchBox">
     <p>
       <input type="text" name="search" id="search"  onkeyup="searching(this.value)" placeholder="Search here">
       <button class="find" data-icon="&#xe986;"></button>
     </p>
   </form>

【问题讨论】:

    标签: ajax search post livesearch


    【解决方案1】:

    你用type : "text", 覆盖你的type : "POST",。所以你必须删除type : "text",,并且从jQuery 1.9开始你应该使用method而不是type

    除此之外,你不应该写data : "search="+string,,而是使用:

    data : {
       search : string
    }
    

    相反,因为这样您就可以确定 string 将始终以正确的方式编码。

    【讨论】:

      【解决方案2】:
      function searching(string){
      
      $.ajax({
           url  : "request/search.php",
           type : "POST",
           data : {'search': string},
           type : "text",
           beforeSend : function(http){
      
          },
      success : function(response,status,http){
           alert(response);
          },
      error : function(http,status,error){
          $('.response').html("<span class='error'>Something went wrong</span>");
          $(".response").slideDown();
         }
        })
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 2013-02-15
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 2016-10-28
        • 2020-09-03
        • 2013-04-22
        • 1970-01-01
        相关资源
        最近更新 更多