Javascript Ajax调用语法
尝试做一个简单的ajax帖子,出于某种原因,它没有发布我的数据!它成功地发布到文件(ajaxpost.php),但没有传递POST数据Javascript Ajax调用语法,javascript,jquery,ajax,Javascript,Jquery,Ajax,尝试做一个简单的ajax帖子,出于某种原因,它没有发布我的数据!它成功地发布到文件(ajaxpost.php),但没有传递POST数据 var myKeyVals = {caption: "test"}; $.ajax({ //Process the form using $.ajax() type : 'POST', //Method type
var myKeyVals = {caption: "test"};
$.ajax({ //Process the form using $.ajax()
type : 'POST', //Method type
url : 'ajaxpost.php', //Your form processing file url
data : myKeyVals, //Forms name
dataType : 'json',
success : function(data) {
if (!data.success) { //If fails
if (data.errors.name) { //Returned if any error from process.php
$('.throw_error').fadeIn(1000).html(data.errors.name); //Throw relevant error
}
} else {
$('#success').fadeIn(1000).append('<p>' + data.posted + '</p>'); //If successful, than throw a success message
}
}
});
var myKeyVals={caption:“test”};
$.ajax({//使用$.ajax()处理表单)
类型:'POST',//方法类型
url:'ajaxpost.php',//您的表单处理文件url
数据:myKeyVals,//表单名称
数据类型:“json”,
成功:功能(数据){
if(!data.success){//if失败
if(data.errors.name){//如果process.php中有任何错误,则返回
$('.throw_error').fadeIn(1000.html(data.errors.name);//抛出相关错误
}
}否则{
$('#success').fadeIn(1000).append(''+data.posted+'');//如果成功,则抛出一条成功消息
}
}
});
这是我的AjaxPost.php
<?php
$text = $_GET['caption'];
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current = "LOG: " . $text . "\n";
// Write the contents back to the file
file_put_contents($file, $current, FILE_APPEND);
?>
在您的php文件中,您使用的是$\u GET['caption']
,但您应该使用$\u POST['caption']
,因为这是一个POST请求。在php中使用$\u POST
访问数据,而不是$\u GET
或者,如果您希望支持这两种HTTP方法,您可以使用$\u REQUEST
您不应该使用$\u POST['caption']
而不是$\u GET['caption']
?