您好,我是使用 jQuery 和 Ajax 的新手。我正在尝试使用 Jquery POST 方法将数据提交到服务器。我传递的数据是一个字符串。现在我无法理解如何传递数据以及如何检索数据。我曾尝试搜索有关我的问题的文章,但没有找到。我相信我的问题非常基本。
if (1)//validateStep(step)
{
if(step==1)
{
var data = document.getElementById('hiddenContact').value;
$.post('/callcenter/admin/postContacts', data);
}
}
现在我将发布我的 postContacts 操作的代码,这不是什么大事。
function postContacts()
{
$this->autoRender = false;
echo '<script>console.log("post contacts");</script>';
}
但我对如何检索数据感到困惑。任何帮助表示赞赏。我正在使用 cakePHP,所以我不得不使用 autoRender=false;这使得 View 可选。
最佳答案
与 jQuery post您可以定义一个回调函数,该函数在返回数据时执行:
$.post('/callcenter/admin/postContacts', data, function(returnedData) {
// do something here with the returnedData
console.log(returnedData);
});
数据
的格式应该是:
{name: 'value', anotherName: 'another value'}
这相当于 PHP 端的帖子名称,可以像这样在普通 PHP 中访问:
echo $_POST['name']; # prints "value"
echo $_POST['anotherName']; # print "another value"
关于php - 将数据从 jQuery 传递到 PHP 以获取 ajax 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4210025/