2016-08-02 60 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(); 
    } 
    }) 
}