干杯斯科特。
的Javascript
$(function() {
$("#saveButton").click(function () {
var name = $('#name').val(),
email = $('#email').val(),
phone = $('#phone').val(),
fb = $('#fb').val(),
tw = $('#tw').val(),
postcode = $('#postcode').val();
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://booglam.local/client/add',
async: false,
data: { name: name, email: email, phone: phone, fb: fb, tw: tw, postcode: postcode },
success: function(json) {
console.log(json.id + ' ' + json.details);
}
});
});
});
主要的PHP控制器是
public function addAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);
$data = $this->_request->getPost();
echo Zend_Json::encode(array('name' => $data['name'], 'email' => $data['email']));
}
答案 0 :(得分:4)
在函数结束时尝试返回false,以防止常规提交过程开始。
$(function() {
$("#saveButton").click(function () {
var name = $('#name').val(),
email = $('#email').val(),
phone = $('#phone').val(),
fb = $('#fb').val(),
tw = $('#tw').val(),
postcode = $('#postcode').val();
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://booglam.local/client/add',
async: false,
data: { name: name, email: email, phone: phone, fb: fb, tw: tw, postcode: postcode },
success: function(json) {
console.log(json.id + ' ' + json.details);
}
});
return false;
});
});