2016-05-29 109 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

对不起,这不起作用。 –