2016-05-29 105 views
-1

我有一個簡單的代碼,但不工作。我想創建一個更復雜的功能,但我只需要基本的結構。Jquery簡單的Ajax Post PHP不工作

HTML

<span class="cleanreport">Delete Record</span> 

的Javascript:

$(".cleanreport").click(function() { 

var token = "test"; 

$.ajax({ 
data: {"data": token}, 
type: "post", 
url: "clean.php", 
success: function (data) { 
console.log(data); 
$('.test').html(data); 
    } 
    }); 
}); 

PHP clean.php

$data = $_POST['data']; 
echo $data; 

我做錯了嗎?

+0

你測試了你的PHP $ _POST。如果它的空試試這個$ tempData = file_get_contents(「php:// input」);打印PHP error_log或轉儲輸出以測試PHP。 –

+0

'type:「post」,'==>'方法:「POST」,' –

+0

我的網站上有其他的ajax調用,發佈和表單正在工作,只有這不起作用。 –

回答

0

這應該爲你工作:

var token = "test"; 
$.ajax({ 
    type: 'POST', 
    url: "clean.php", 
    data: {id: token}, 
    dataType: "json", 
    success: function(response) { 
     // some debug could be here 
    }, 
    error: function(a,b,c) { 
     // some debug could be here 
    } 
}); 

如果沒有,請使用調試console.log()successerror參數。

基於jquery documentation設置typemethod的別名,因此這在您的情況下肯定不會成爲問題。

+0

對不起,但仍然無法正常工作。我試着用console.log但沒有顯示,也沒有錯誤error.log –

+0

確定,只需要在$(function(){....})中添加ajax函數;謝謝 –

0
$(".cleanreport").click(function() { 
    var token = "test"; 
    $.post('clean.php", 
    { 
     data: token 
    }, 
    function (data,status) { 
     //console.log(data); 
     $('.test').html(data); 
    }); 
}); 
+0

對不起,這不起作用。 –