简体   繁体   English

使用Javascript进行ASP.NET MVC Ajax调用

[英]Making an ASP.NET MVC Ajax call with the use of Javascript

First off, thanks for taking the time to read. 首先,感谢您抽出宝贵的时间阅读。

I'm trying to delve into ASP.NET MVC at the moment, however i currently have no wish to use any type of JavaScript framework, so please, don't tell me how much easier it would be etc, in your answer. 目前,我正在尝试研究ASP.NET MVC,但是我目前不希望使用任何类型的JavaScript框架,因此,请不要在回答中告诉我它有多容易。

I currently have a Javascript function that successfully makes an AJAX call, however i am struggling to understand why no values are being returned from the request. 我目前有一个可以成功进行AJAX调用的Javascript函数,但是我很难理解为什么请求中没有返回任何值。

The function is as follows. 功能如下。

function ajaxRequestUser(num) {
var ajax;

try {
    ajax = new XMLHttpRequest();
} catch(e) {
    try {
        ajax = new ActiveXObject(Msxml2.XMLHTTP);
    } catch(e){
        alert('old browser');
    }
}

ajax.readystatechange = function () {
    if (ajax.readyState == 4) {
        var queryResult = ajax.responseText;

        if (!queryResult) {
            alert('No Information.');
        } else {
            alert(queryResult);
        }
    }
}
   var requestString = "?user="+num;
   ajax.open("GET", "/Users/GetUser" + requestString, true);
   ajax.send(null);
}

The function is called via a separate function that simply does some UI modifications to allow for the display of the data. 该函数是通过一个单独的函数调用的,该函数仅进行一些UI修改即可显示数据。

The alerts are there at this point, because i was not receiving any data back from the call and i was testing to see if that part of the code was being hit at all (Don't go into the differences between Synchronous and Asynchronous.). 此时有警报,因为我没有从调用中收到任何数据,并且我正在测试以查看部分代码是否全部被击中(不要进入同步和异步之间的区别。) 。 No matter how long i waited the data being returned was not being returned, after breaking through the actual server side c#, i saw the data being sent back, but it was just never being received. 无论我等了多久才返回的数据都没有返回,在突破了实际的服务器端c#之后,我看到了正在发送回的数据,但从未收到过。 Is there something in the code that was done wrong? 代码中是否有错误的地方? Or am i going about receiving the inbound data in the wrong way? 还是我会以错误的方式接收入站数据?

我发现了与我的代码有关的问题,并且这仅仅是基于我的ajax.event声明的错误,其中该事件是onreadystatechange而不是readystatechange

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

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