<!DOCTYPE html> <html> <head> <title>XMLHttpRequest Demo</title> </head> <body> <h1>XMLHttpRequest Demo</h1> <p id="response"></p> <script> window.onload = function () { interceptRequest(); makeRequest(); }; function interceptRequest() { var xhr = new XMLHttpRequest(); var originalOpen = xhr.open; xhr.open = function(method, url, async) { console.log("拦截原请求,发送新请求..."); var newUrl = "https://example.com/new-api"; originalOpen.call(this, method, newUrl, async); }; xhr.send(); } function makeRequest() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { document.getElementById("response").innerHTML = xhr.responseText; } }; xhr.open( "GET", "http://finance-sit.test.hihonor.com/fin/eipm/dom/servlet/environment", true ); xhr.send(); } </script> </body> </html>
时间: 2024-02-15 08:28:50 浏览: 30
这段代码是一个使用 XMLHttpRequest 对象进行 AJAX 请求的示例。其中,makeRequest 函数使用 XMLHttpRequest 对象发出一个 GET 请求,并将响应的内容显示在页面上。而 interceptRequest 函数使用 XMLHttpRequest 对象拦截原本的请求,将其重定向到一个新的 URL 上。当拦截到原请求时,该函数会打印一条消息并发送一个新的请求。
相关问题
<!DOCTYPE html> <html> <head> <title>XMLHttpRequest Demo</title> </head> <body> <h1>XMLHttpRequest Demo</h1> <p id="response"></p> <script> window.onload = function () { makeRequest(); }; function makeRequest() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { document.getElementById("response").innerHTML = xhr.responseText; } }; xhr.open( "GET", "http://finance-sit.test.hihonor.com/fin/eipm/dom/servlet/environment", true ); xhr.send(); } </script> </body> </html>
这是一段使用 XMLHttpRequest 对指定 URL 进行 GET 请求的代码。在代码中,通过创建 XMLHttpRequest 对象,设置 onreadystatechange 事件监听器来监听请求状态的变化。当请求状态变为 4(表示请求已完成)且状态码为 200(表示请求成功),通过获取响应的文本内容,将其显示在指定的 HTML 元素中。此代码将从指定 URL 获取数据并在页面上显示结果。
<!DOCTYPE html> <html> <head> <title>XMLHttpRequest Demo</title> </head> <body> <h1>XMLHttpRequest Demo</h1> <button onclick="makeRequest()">发送请求</button> <p id="response"></p> <script> function makeRequest() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { document.getElementById("response").innerHTML = xhr.responseText; } }; xhr.open("GET", "http://example.com/api/data", true); xhr.send(); } function interceptRequest() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { document.getElementById("response").innerHTML = xhr.responseText; } }; // 拦截原始请求并发送新请求 var originalOpen = xhr.open; xhr.open = function(method, url, async, user, password) { originalOpen.call(this, method, "http://example.com/api/newdata", async, user, password); }; xhr.send(); } </script> </body> </html>
这段代码是一个利用 XMLHttpRequest 发送 HTTP 请求的示例。当用户点击“发送请求”按钮时,会向 http://example.com/api/data 发送 GET 请求,并将响应的内容显示在页面上。另外还定义了一个名为 interceptRequest 的函数,可以用来拦截原始请求并发送新的请求。具体来说,它会在 xhr.open 方法被调用之前,将原始请求的 URL 修改为 http://example.com/api/newdata,然后再发送请求获取数据。
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)