我试图用ajax运行PHP文件,并从PHP文件的文本输出到我的容器。我已经研究了多个例子,但仍然没有从我的ListTranslations.php文件中得到任何东西。
对将要发生的事情的简短解释:
在index.php文件上,我运行数据库中找到的所有语言的列表,并用特定的语言填充onClick
函数。单击后,ajax函数应该运行我的ListTranslations.php文件,并输出为特定语言找到的所有翻译的列表。
这里是ajax函数调用的index.php:
foreach($languages as $language){
echo '<a href="index.php?id='. $language . '"target="_self" onclick="sendLanguage(''' .$language. ''')">' . $language . '</a>' . ': ' . count($language) . "<br>";
}
ajax函数:(我有一个内容div集)
function sendLanguage(tongue) {
jQuery.ajax({
url: "ListTranslations.php",
method: 'GET',
data: {language: tongue},
contentType: 'application/json; charset=utf-8',
dataType: 'json'
}).done(function(r){
$('#content').html(response);
}).fail(function(e){
console.log(e);
});
}
ListTranslations.php文件:
if($_GET["tongue"]) {
$aPoem = $poems->find(array('language'=>($_GET["tongue"])));
foreach($aPoem as $poem) {
echo '<a href="testi.php?id=' . $poem["_id"] . '" target="_self">' . $poem["title"] .'</a><br>';
}
}
我不知道我做错了什么,为什么它不工作,所以任何帮助是非常感激!
首先,声明你的div是class还是id content:
$(".content");
$("#content");
另外,你的函数参数不是在整个结构中使用,你只使用'舌'字符串;要传递一个变量,去掉like:
data: { language: tongue }
还有,使用已弃用的 jQuery方法有什么特别的原因吗?如果没有,你应该使用这样的结构:
function findDifferentNameThanAjax(tongue) {
$.ajax({
url: "ListTranslations.php",
method: 'GET',
data: {language: tongue},
contentType: 'application/json; charset=utf-8',
dataType: 'json'
}).done(function(r){
$('#content').html(response);
}).fail(function(e){
console.log(e);
});
}