0

我再次在我的代码中使用 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>
4

2 回答 2

0

你用 覆盖你type : "POST",type : "text",。所以你必须删除type : "text",,因为 jQuery 1.9 你应该使用method而不是type.

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

data : {
   search : string
}

取而代之的是,因为这样您就可以确定string始终以正确的方式对其进行编码。

于 2016-08-02T11:17:36.910 回答
0
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();
   }
  })
}
于 2016-08-02T13:20:41.843 回答