Hello I am a newbie working with jQuery and Ajax. I am trying submit data to the server using Jquery POST method. And the data that I am passing is a string. Now I am unable to understand how do I pass the data and how do I retrieve the data. I have tried searching for articles for my problem, but I have found none. I believe my problem is very basic.
你好,我是一个使用jQuery和Ajax的新手。我正在尝试使用Jquery POST方法将数据提交到服务器。我传递的数据是一个字符串。现在我无法理解如何传递数据以及如何检索数据。我试过为我的问题搜索文章,但我没找到。我相信我的问题非常基础。
if (1)//validateStep(step)
{
if(step==1)
{
var data = document.getElementById('hiddenContact').value;
$.post('/callcenter/admin/postContacts', data);
}
}
Now I'll post the code for my postContacts action, which isn't a big thing.
现在我将发布我的postContacts动作的代码,这不是一件大事。
function postContacts()
{
$this->autoRender = false;
echo '<script>console.log("post contacts");</script>';
}
But I am confused as to how the data has to be retrieved. Any help is appreciated. I am using cakePHP, so I have had to use autoRender=false; which makes the view optional.
但我对如何检索数据感到困惑。任何帮助表示赞赏。我正在使用cakePHP,所以我不得不使用autoRender = false;这使视图可选。
3 个解决方案
#1
16
With jQuery post you can define a callback function which is executed when the data is returned:
使用jQuery post,您可以定义一个回调函数,该函数在返回数据时执行:
$.post('/callcenter/admin/postContacts', data, function(returnedData) {
// do something here with the returnedData
console.log(returnedData);
});
The data
should be in the form:
数据应采用以下形式:
{name: 'value', anotherName: 'another value'}
which equates to the post names on the PHP end accessible in plain PHP like this:
这相当于PHP端的帖子名称可以在普通的PHP中访问,如下所示:
echo $_POST['name']; # prints "value"
echo $_POST['anotherName']; # print "another value"
#2
2
The data param is supposed to be an object that has keys and values.
数据参数应该是具有键和值的对象。
var data = {
hiddenContact: document.getElementById('hiddenContact').value
}
$.post('/callcenter/admin/postContacts', data);
Then in PHP you can retrieve it like this:
然后在PHP中,您可以像这样检索它:
$hiddenContact = $_POST["hiddenContact"];
I'm not a big CakePHP user but I believe the CakePHP version is like this:
我不是一个很大的CakePHP用户,但我相信CakePHP版本是这样的:
$hiddenContact = $this->params["hiddenContact"];
#3
1
//javascript
if(step==1)
{
var data = {'MyFieldName':document.getElementById('hiddenContact').value};
$.post('/callcenter/admin/postContacts', data, function(returnData){
alert('The server said ' + returnData);
});
}
//read the post in php
<?
echo 'Your ajax post data was '. $_POST['MyFieldName'];
?>
#1
16
With jQuery post you can define a callback function which is executed when the data is returned:
使用jQuery post,您可以定义一个回调函数,该函数在返回数据时执行:
$.post('/callcenter/admin/postContacts', data, function(returnedData) {
// do something here with the returnedData
console.log(returnedData);
});
The data
should be in the form:
数据应采用以下形式:
{name: 'value', anotherName: 'another value'}
which equates to the post names on the PHP end accessible in plain PHP like this:
这相当于PHP端的帖子名称可以在普通的PHP中访问,如下所示:
echo $_POST['name']; # prints "value"
echo $_POST['anotherName']; # print "another value"
#2
2
The data param is supposed to be an object that has keys and values.
数据参数应该是具有键和值的对象。
var data = {
hiddenContact: document.getElementById('hiddenContact').value
}
$.post('/callcenter/admin/postContacts', data);
Then in PHP you can retrieve it like this:
然后在PHP中,您可以像这样检索它:
$hiddenContact = $_POST["hiddenContact"];
I'm not a big CakePHP user but I believe the CakePHP version is like this:
我不是一个很大的CakePHP用户,但我相信CakePHP版本是这样的:
$hiddenContact = $this->params["hiddenContact"];
#3
1
//javascript
if(step==1)
{
var data = {'MyFieldName':document.getElementById('hiddenContact').value};
$.post('/callcenter/admin/postContacts', data, function(returnData){
alert('The server said ' + returnData);
});
}
//read the post in php
<?
echo 'Your ajax post data was '. $_POST['MyFieldName'];
?>