我需要在点击链接后发送AJAX查询,PHP返回json,我需要在jQuery中输出。
但我找不到错误,php文件中的var_dump($_POST)
返回空数组,主要问题 - 为什么$_SERVER['REQUEST_METHOD']
返回GET?
的script.js
$(document).ready(function(){
$('.js').click(function(event){
event.preventDefault();
var item = $(this).html();
$.ajax({
dataType: "json",
method: "POST",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
url: '/web/4/god/core/ajax.php',
data: {article: item},
success: function(result){
$('.content').html(result.content);
}
});
});
});
ajax.php返回正确的json对象,但只有当我使用$_GET
而不是$_POST
时,它总是“物理”,所以据我所知,这意味着PHP无法找到我的数据我发送。
ajax.php
if (!empty($_GET['article'])) {
$topic = $_GET['article'];
} else {
$topic = 'physics';
}
$result = array();
$query = "SELECT `text`
FROM content
WHERE topic='" . $topic . "'";
if (!$query) {
die("loh");
}
$get = mysql_query($query);
if (!$get) {
die (mysql_error());
}
while ($arr = mysql_fetch_array($get)) {
$result['topic'] = $topic;
$result['content'] = htmlspecialchars($arr["text"],ENT_SUBSTITUTE);
}
$json = json_encode($result);
echo $json;
Thanx很多!
P.S。 Here it returns POST