2016-08-02 61 views
0

再一次,我在我的代码中卡住了ajax。我正在为我的项目进行实时搜索ajax系统,但是当我将数据发送到search.php & var_dump POST数组时,我得到的是空数组。通过ajax发送数据后,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> 

回答

0

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

旁边,你不应该写data : "search="+string,但使用:

data : { 
    search : string 
} 

代替,因为这样你可以肯定的是string将永远编码的正确方法。

+0

oop的感谢我没注意到 –

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(); 
    } 
    }) 
}