将数据从jQuery传递到PHP以获取ajax帖子

mac*_*cha 7 javascript php ajax jquery post

你好我是一个使用jQuery和Ajax的新手.我正在尝试使用Jquery POST方法将数据提交到服务器.我传递的数据是一个字符串.现在我无法理解如何传递数据以及如何检索数据.我曾尝试为我的问题搜索文章,但我没有找到.我相信我的问题非常基本.

if (1)//validateStep(step)
{
if(step==1)
{
var data = document.getElementById('hiddenContact').value;
$.post('/callcenter/admin/postContacts', data);
}
}
Run Code Online (Sandbox Code Playgroud)

现在我将发布我的postContacts动作的代码,这不是一件大事.

function postContacts()
{
$this->autoRender = false;
echo '<script>console.log("post contacts");</script>';
}
Run Code Online (Sandbox Code Playgroud)

但我对如何检索数据感到困惑.任何帮助表示赞赏.我正在使用cakePHP,所以我不得不使用autoRender = false; 这使视图可选.

Mar*_*row 16

使用jQuery post,您可以定义一个回调函数,该函数在返回数据时执行:

$.post('/callcenter/admin/postContacts', data, function(returnedData) {
    // do something here with the returnedData
    console.log(returnedData);
});
Run Code Online (Sandbox Code Playgroud)

data应在形式:

{name: 'value', anotherName: 'another value'}
Run Code Online (Sandbox Code Playgroud)

这相当于PHP端的帖子名称可以在普通的PHP中访问,如下所示:

echo $_POST['name'];           # prints "value"
echo $_POST['anotherName'];    # print "another value"
Run Code Online (Sandbox Code Playgroud)