【问题标题】:i am unable to run ajax example我无法运行 ajax 示例
【发布时间】:2011-04-21 06:31:57
【问题描述】:

大家好,我有以下代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
 <script type="text/javascript">
 function showHint(str)
 {
    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('hint').innerHTML= xmlhttp.responseText;
        }
        }
        xmlhttp.open("GET","sample.aspx?q=" + str ,true)
        xmlhttp.send()
 }
 </script>
 </head>
 <body>
Type here: <input type="text" id="txt" onKeyUp = "showHint(this.value)"/>  
Suggestion here: <div id="hint"></div>
 </body>
</html>

但是这个例子不起作用..它说访问被拒绝(脚本错误) 如何解决这个..! 我的 aspx 页面如下所示

<%
response.expires=-1
dim a(30)
'Fill up array with names
a(1)="Anna"
a(2)="Brittany"
a(3)="Cinderella"
a(4)="Diana"
a(5)="Eva"
a(6)="Fiona"
a(7)="Gunda"
a(8)="Hege"
a(9)="Inga"
a(10)="Johanna"
a(11)="Kitty"
a(12)="Linda"
a(13)="Nina"
a(14)="Ophelia"
a(15)="Petunia"
a(16)="Amanda"
a(17)="Raquel"
a(18)="Cindy"
a(19)="Doris"
a(20)="Eve"
a(21)="Evita"
a(22)="Sunniva"
a(23)="Tove"
a(24)="Unni"
a(25)="Violet"
a(26)="Liza"
a(27)="Elizabeth"
a(28)="Ellen"
a(29)="Wenche"
a(30)="Vicky"

'get the q parameter from URL
q=ucase(request.querystring("q"))

'lookup all hints from array if length of q>0
if len(q)>0 then
  hint=""
  for i=1 to 30
    if q=ucase(mid(a(i),1,len(q))) then
      if hint="" then
        hint=a(i)
      else
        hint=hint & " , " & a(i)
      end if
    end if
  next
end if

'Output "no suggestion" if no hint were found
'or output the correct values
if hint="" then
  response.write("no suggestion")
else
  response.write(hint)
end if
%>

【问题讨论】:

    标签: asp.net ajax


    【解决方案1】:

    您有两个不同的问题。首先,您的 sample.aspx 应该是 sample.asp,因为它是经典的 asp 页面,而不是 .Net asp 页面。确保您也更改了 xmlhttp.open 方法中的路径。

    其次,xmlhttp.readystate 应该是 xmlhttp.readyState - 注意大写的 S。我花了一点时间才弄清楚那部分。

    【讨论】:

      【解决方案2】:


      问题可能是您在 file:// 协议中运行 HTML 文件。据我所知,像 .php 和 .asp 这样的服务器文件在 file:// 协议中不起作用。
      如果您真的希望它起作用,请尝试设置 Apache 并将您的文件放入您的服务器文件夹中。如果你在 Linux 中,它在 /var/www 中,我不太确定其他操作系统。
      另外,我认为这无关紧要,我建议将您的 Doctype 更改为 &lt;!DOCTYPE html&gt;,因为它现在是标准。对不起。你不必。
      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-29
        相关资源
        最近更新 更多