简体   繁体   English

从HTML到ASPX的AJAX调用?

[英]AJAX call from HTML to ASPX?

if(window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

request.open('GET', 'foo.php?bar=' + baz, true);
request.send()

I know that this can be done with PHP, but I'd like to know if/how it can be done with aspx (ie request.open('GET', 'foo.aspx?bar=' + baz, true); ). 我知道这可以用PHP完成,但是我想知道是否/如何用aspx完成(即request.open('GET', 'foo.aspx?bar=' + baz, true); )。 In the example above, the AJAX call is being made in javascript from a plain old HTML page (not PHP), but it can interact with PHP. 在上面的示例中,AJAX调用是通过javascript从一个普通的旧HTML页面(不是PHP)进行的,但是它可以与PHP交互。

Can the same thing be done with aspx? aspx可以做同样的事情吗? If so, do I need to target the code behind file or not? 如果是这样,我是否需要针对文件后面的代码? Say that the aspx is only there to respond to requests like this, is there something to put in the aspx file that automatically passes the GET to the vb/cs file? 假设只有aspx可以响应这样的请求,在aspx文件中是否有东西可以自动将GET传递给vb / cs文件?

Any help would be appreciated. 任何帮助,将不胜感激。 I'm just more accustomed to working with PHP, but my current job is primarily a VB shop, so PHP comparisons are welcomed. 我只是习惯于使用PHP,但是我目前的工作主要是在VB商店中工作,因此欢迎进行PHP比较。 Thanks. 谢谢。

Ajax just means "Making an HTTP request from JavaScript without leaving the page". Ajax的意思是“不离开页面就通过JavaScript发出HTTP请求”。

There is nothing special about the HTTP request. HTTP请求没有什么特别的。

The server can generate the response in any way you like. 服务器可以按照您喜欢的任何方式生成响应。 Static files, Perl, JavaScript (via Node), PHP, ASP.NET, any way you like. 静态文件,Perl,JavaScript(通过节点),PHP,ASP.NET,任何您喜欢的方式。

Aspx was not meant to work this way. Aspx并不是要以这种方式工作。

You will need to catch the ajax call on the Load event of the page, and then Response.Write whatever you want to return as a string. 您将需要在页面的Load事件上捕获ajax调用,然后捕获Response.Write任何要作为字符串返回的内容。 Then you have to set the Response.ContentType and then you need to end it with Response.End. 然后,您必须设置Response.ContentType,然后以Response.End结尾。

It is complicated and a bit of a hack. 这很复杂,有点骇人听闻。

Your best option is to start learning asp net mvc which will most likely work similarly to any php mvc framework. 最好的选择是开始学习asp net mvc,这很可能类似于任何php mvc框架。

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

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