简体   繁体   English

在ASP.NET MVC中使用JavaScript XMLHttpRequest()

[英]using JavaScript XMLHttpRequest() with ASP.NET MVC

I am trying to test using the JavaScript XMLHttpRequest() object in an ASP.NET MVC application. 我正在尝试在ASP.NET MVC应用程序中使用JavaScript XMLHttpRequest()对象进行测试。 I have written the following test code which I put into a View - 我编写了以下测试代码,并放入了View中-

<script type="text/javascript">
var request;

function getAJAX() {
    request = new XMLHttpRequest();
    request.open("GET", "test.txt");
    request.onreadystatechange = checkData();
    request.send(null);
}


function checkData() {
    if (request.readyState == 4) { 
        if (request.status == 200) { 
            alert(request.responseText);                
        }
    }
}
</script>

<form action="">
    <p>
        <button type="button" onclick="getAJAX()">DO IT!</button>
    </p>
</form> 

When I click in the "DO IT!" 当我单击“ DO IT!”时 button, the script functions get called, but the "request.onreadystatechange" never changes. 按钮,脚本函数被调用,但是"request.onreadystatechange"从不改变。 I have a few questions about this - 我对此有一些疑问-

  1. Is there a simple way to trace what is happening with the request.open() call? 有没有一种简单的方法来跟踪request.open()调用发生了什么?
  2. Will the XMLHttpRequest() object even work in an ASP.NET MVC application? XMLHttpRequest()对象甚至可以在ASP.NET MVC应用程序中工作吗?
  3. Will I have to make changes to the Global.asax file (or other changes) to make this work? 我是否需要对Global.asax文件进行更改(或其他更改)才能使其正常工作?
  4. I have "test.txt" in the "base" directory (the same location as the Global.asax file) is that where the request.open call will look? 我在"base"目录(与Global.asax文件位于相同的位置)中具有"test.txt" ,这是request.open调用的外观?

( NOTE : I am trying to do this without jQuery) 注意 :我正在尝试不使用jQuery来执行此操作)

Thanks very much! 非常感谢!

You're setting the callback to onreadystatechange to the result of executing checkData . 您正在将回调设置为onreadystatechange以执行checkData结果 Set onreadystatechange equal to checkData instead and you should be good to go: onreadystatechange设置为等于checkData ,那么您应该可以进行以下操作:

function getAJAX() {
    request = new XMLHttpRequest();
    request.open("GET", "test.txt");
    request.onreadystatechange = checkData; // Don't execute checkData
    request.send(null);
}

As for your other questions: 至于您的其他问题:

Is there a simple way to trace what is happening with the request.open() call? 有没有一种简单的方法来跟踪request.open()调用发生了什么?

Use Firebug or the development tool of your choice to inspect AJAX requests. 使用Firebug或您选择的开发工具来检查AJAX请求。

Will the XMLHttpRequest() object even work in an ASP.NET MVC application? XMLHttpRequest()对象甚至可以在ASP.NET MVC应用程序中工作吗?

Yes, it'll work fine. 是的,它将正常工作。

Will I have to make changes to the Global.asax file (or other changes) to make this work? 我是否需要对Global.asax文件进行更改(或其他更改)才能使其正常工作?

Not that I'm aware of. 不是我知道的。

I have "test.txt" in the "base" directory (the same location as the Global.asax file) is that where the request.open call will look? 我在“基本”目录(与Global.asax文件位于相同的位置)中具有“ test.txt”,这是request.open调用的外观?

Maybe? 也许? It really depends on your routing rules and how you have IIS configured. 这实际上取决于您的路由规则以及如何配置IIS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM