从Javascript OnClick调用PHP页面时AJAX返回错误
我的解决方案是使用onClick事件调用PHP页面,使用AJAX发布参数并返回结果。我在使用指定的URL调用PHP页面时插入参数。 以下是我的Javascript:从Javascript OnClick调用PHP页面时AJAX返回错误,php,javascript,html,ajax,json,Php,Javascript,Html,Ajax,Json,我的解决方案是使用onClick事件调用PHP页面,使用AJAX发布参数并返回结果。我在使用指定的URL调用PHP页面时插入参数。 以下是我的Javascript: function uploadContact() { var name, email, phone, badgeid; if(window.localStorage.length > 0) { name = window.localStorage.getI
function uploadContact() {
var name, email, phone, badgeid;
if(window.localStorage.length > 0)
{
name = window.localStorage.getItem('name');
email = window.localStorage.getItem('email');
phone = window.localStorage.getItem('phone');
}
else
{
name = $('input[name=fullname]').val();
email = $('input[name=email]').val();
phone = $('input[name=phone]').val();
}
$.ajax({
url: 'UpdateContactList.php?name=' + name + '&email=' + email + '&phone=' + phone,
type: $form.attr('method'),
dataType: 'json',
success: function(responseJson) {
$form.before("<p>"+responseJson.message+"</p>");
},
error: function() {
$form.before("<p>There was an error processing your request.</p>");
}});
}
函数上传联系人(){
var名称、电子邮件、电话、徽章ID;
如果(window.localStorage.length>0)
{
name=window.localStorage.getItem('name');
email=window.localStorage.getItem('email');
phone=window.localStorage.getItem('phone');
}
其他的
{
name=$('input[name=fullname]')。val();
email=$('input[name=email]')。val();
phone=$('input[name=phone]')。val();
}
$.ajax({
url:'UpdateContactList.php?name='+name+'&email='+email+'&phone='+phone,
类型:$form.attr('method'),
数据类型:“json”,
成功:功能(responseJson){
$form.before(“”+responseJson.message+””);
},
错误:函数(){
$form.before(“处理您的请求时出错。”;
}});
}
获取参数的PHP代码:
<?php
$response = array();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// if form has been posted process data
// you dont need the addContact function you jsut need to put it in a new array
// and it doesnt make sense in this context so jsut do it here
// then used json_decode and json_decode to read/save your json in
// saveContact()
$data = array( 'fullname' => $_POST['name'], 'email' => $_POST['email'], 'phone' => $_POST['phone']);
//These line is for testing only, remove it when done testing
$test = $_POST['name'] . ' ' . $_POST['email'];
echo "<script>alert('$test');</script>";
// always return true if you save the contact data ok or false if it fails
$response['status'] = updateContact($data) ? 'success' : 'error';
$response['message'] = $response['status']
? 'Your submission has been saved!'
: 'There was a problem saving your submission.';
header("Content-type: application/json");
echo json_encode($response);
exit;
}
...
function updateCacheFile($filename)
{
$filename = "contact";
$filename = $filename . ".appcache";
$cachefile = fopen ($filename, "a+");
....
file_put_contents($filename, $cache_content);
}
$.ajax({
url:'UpdateContactList.php',
数据:{'name':name,'email':email,'phone':phone,'badgeid':badgeid},
类型:$form.attr('method'),
数据类型:“json”,
成功:功能(responseJson){
$form.before(“”+responseJson.message+””);
},
错误:函数(){
$form.before(“处理您的请求时出错。”;
}});
尝试在ajax的数据参数中发送信息,并将类型设置为post。像这样
$.ajax({
url: 'UpdateContactList.php',
type: 'post',
dataType: 'json',
data: {'key1': value1, 'key2':value2},
success: function(responseJson) {
$form.before("<p>"+responseJson.message+"</p>");
},
$.ajax({
url:'UpdateContactList.php',
键入:“post”,
数据类型:“json”,
数据:{'key1':value1,'key2':value2},
成功:功能(responseJson){
$form.before(“”+responseJson.message+””);
},
我现在明白了。关于这个案例的更多问题是,所有的事情都做得正确,文件都写好了,参数都通过了-我删除了测试行,不再需要echo了-但有一件事:我总是在AJAX返回中得到消息“处理您的请求时出错”。这是怎么回事?
header("Content-type: application/json");
echo "<script>alert('$test');</script>";
// always return true if you save the contact data ok or false if it fails
$response['status'] = updateContact($data) ? 'success' : 'error';
$response['message'] = $response['status']
? 'Your submission has been saved!'
: 'There was a problem saving your submission.';
echo json_encode($response);
$.ajax({
url: 'UpdateContactList.php',
data:{'name':name,'email': email,'phone':phone,'badgeid ':badgeid },
type: $form.attr('method'),
dataType: 'json',
success: function(responseJson) {
$form.before("<p>"+responseJson.message+"</p>");
},
error: function() {
$form.before("<p>There was an error processing your request.</p>");
}});
$.ajax({
url: 'UpdateContactList.php',
type: 'post',
dataType: 'json',
data: {'key1': value1, 'key2':value2},
success: function(responseJson) {
$form.before("<p>"+responseJson.message+"</p>");
},