AJAX / ASP - 简单的任务但我被困住了?
我正在尝试对 ASP 页面执行一个简单的 AJAX 调用,该调用会重置会话变量并在完成时发回一条小消息。我这样做纯粹是为了学习 AJAX。
该示例来自 W3 Schools 网站,但自从将其应用到我的页面后,我似乎无法让它工作,并且没有产生任何错误,这很烦人,因为我无法调试它。
这是我的 JS,当用户点击按钮时调用 [清除表单]:
function resetSearchForm()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("notification").innerHTML=xmlhttp.responseText;
document.getElementById('notification').style.visibility = 'visible';
}
}
xmlhttp.open("GET","clearSearchData.asp",true);
xmlhttp.send();
document.searchFrm.searchStr.value='';
document.searchFrm.vertical.checked = true;
document.searchFrm.horizontal.checked = true;
document.getElementById('dateRange').selectedIndex = 0;
document.searchFrm.searchStr.focus();
}
这是 ASP (clearSearchData.asp),它清除我的会话变量并写入一条消息:
Response.Expires = -1
Session("search-str-boolean") = ""
Session("search-str-plain") = ""
Session("date-range") = ""
Session("date-from") = ""
Session("date-to") = ""
Session("specificDate") = ""
Session("peopleStr") = ""
Session("orientation") = ""
Response.Write "Form has been reset"
有人能看到我哪里出错了吗?我看了好久,就是看不出来。
函数本身可以工作,因为函数的最后一部分得到处理,即清除表单值的位...但是...AJAX 调用不会发生,因为会话变量仍然包含数据并且消息没有出现。
非常感谢...
更新 - - - - - - - - - - -
现在可以了。问题是我没有包含 ASP 页面的完整 URL。感谢“thedaian”(如下)指出这一点
I'm trying to do a simple AJAX call to an ASP page, that resets session variables and posts a little message back on completion. I'm doing this purely to learn AJAX.
The example came from W3 Schools website but since applying it to my page, I can't seem to get it to work and it's not producing any errors, which is annoying, because I can't debug it.
This is my JS, which is called when a user hits a button [Clear Form]:
function resetSearchForm()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("notification").innerHTML=xmlhttp.responseText;
document.getElementById('notification').style.visibility = 'visible';
}
}
xmlhttp.open("GET","clearSearchData.asp",true);
xmlhttp.send();
document.searchFrm.searchStr.value='';
document.searchFrm.vertical.checked = true;
document.searchFrm.horizontal.checked = true;
document.getElementById('dateRange').selectedIndex = 0;
document.searchFrm.searchStr.focus();
}
And this is the ASP (clearSearchData.asp) that clears my session variables and writes a message:
Response.Expires = -1
Session("search-str-boolean") = ""
Session("search-str-plain") = ""
Session("date-range") = ""
Session("date-from") = ""
Session("date-to") = ""
Session("specificDate") = ""
Session("peopleStr") = ""
Session("orientation") = ""
Response.Write "Form has been reset"
Can anybody see where I'm going wrong? I have been looking at it for a long time and I just can't see it.
The function itself works because the last part of the function gets processed, the bit that clears the form values... but... the AJAX call doesn't happen because the session variables still contain data and the message doesn't appear.
Many thanks in advance...
UPDATE - - - - - - - - - - -
It now works. The problem was I didn't include the full URL to the ASP page. Thanks for 'thedaian' (below) for pointing that out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试通过 AJAX 获取的页面很可能有问题。检查
xmlhttp.status
是什么,如果是 404,那么您永远不会到达打印 AJAX 响应的位置。确保可以从与 JavaScript 相同的目录访问"clearSearchData.asp"
。如果您的 javascript 代码与站点的其余部分位于单独的文件夹中,则这是一个常见问题。或者只需输入"clearSearchData.asp"
的完整 URL 路径,这样它就一定可以工作。需要指出的是,
xmlhttp.onreadystatechange
中的函数(通常)在代码中声明后被调用。在这种情况下,它会在您的搜索表单字段被清除并重置后被调用。Chances are, something is wrong with the page you're trying to get via AJAX. Check what
xmlhttp.status
is, if it's 404, then you're never going to get to the point where you're printing the AJAX response. Make sure that"clearSearchData.asp"
is accessible from the same directory as your javascript. This is a common problem if you have your javascript code in a separate folder from the rest of your site. Or simply put in the full URL path for the"clearSearchData.asp"
so it'll definitely work.Something to point out, the function in
xmlhttp.onreadystatechange
is (usually) called after it's declared in the code. In this case, it gets called after your search form fields are cleared out and reset.ajax 调用不会自动发送会话 cookie。这意味着您要清除的会话不是用户的会话,而只是单独为该 ajax 调用创建的会话。
The ajax call does not automatically send along a session cookie. That means the session you're clearing is not the user's session, but just a session that's been created for that ajax call alone.