尝试做一个简单的ajax发布,由于某种原因,它没有发布我的数据!它成功发布到文件(ajaxpost.php),但没有传递 POST 数据..
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
}
}
});
这是我的 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 文件中,您使用的是 $_GET['caption']
但您应该使用 $_POST['caption']
因为这是一个发布请求.
关于javascript - Ajax 调用语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595308/