通过ajax调用php文件不起作用 [英] call to php file via ajax not working
问题描述
这似乎是一个相对简单的问题,对于使用ajax来说我还很陌生,但是我看到了一篇有关触发OnClick事件时能够调用PHP脚本的文章.我试图破坏用户单击注销"链接后的会话,因此我想在发生OnClick事件时调用logout.php
文件.我的问题是,单击链接后什么也没发生.
This seems like a relatively simple question, and I am pretty new to using ajax, but I saw a post about being able to call a PHP script when an OnClick event has been triggered. I am trying to destroy a session upon a user clicking the "Log Out" link, so I want to call the logout.php
file when the OnClick event has occurred. My problem is that nothing happens when the link is clicked.
这是我的代码:
$(document).ready(function() {
// Expand Panel
$("#open").click(function(){
//when the user clicks the logout button
$.ajax({
url: "logout.php"
)};
});
});
<div class="tab">
<ul class="login">
<li class="left"> </li>
<li><?php if(isset($_SESSION['username'])){echo $_SESSION['username'];}else{echo 'Hello Guest!';}?></li>
<li class="sep">|</li>
<li id="toggle">
<a id="open" class="open" href="#">Log Out</a>
</li>
<li class="right"> </li>
</ul>
</div> <!-- / top -->
推荐答案
我想您希望显示logout.php文件的输出.我认为您的代码可能正在工作,但是您忘记了告诉ajax将接收到的数据放在何处.
I suppose that you expect the output of the logout.php file to show. I think that your code might be working but that you forgot to tell the ajax where to put the data that it receives.
尝试这样的事情:
$.ajax({
url: 'logout.php',
success: function(data) {
$('.result').html(data);
alert('Logout completed.');
}
});
结果"指向您要将响应加载到的div.
Where "result" points to the div you want to load the response into.
这篇关于通过ajax调用php文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!