繁体   English   中英

我的html文件未响应express.js应用程序中的ajax请求

[英]My html file is not responding to ajax request in express.js application

在我的快速应用程序中,我的根文件app.js在“ a”文件夹中呈现a.html,但a.html没有响应任何用ajax.js编写的ajax代码。 我在下面显示了这些文件-

app.js

 var express = require('express'); var app = express(); app.set(express.static('./a')); app.get('/', function(req, res) { res.sendFile(__dirname + '/a/a.html'); }); app.listen(3000, '127.0.0.1'); 

a.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button id="button"></button> <script src="ajax.js"></script> </body> </html> 

ajax.js

 document.getElementById('button').addEventListener('click', trigger); function trigger() { var xhr = XMLHttpRequest(); xhr.open('GET', 'blah.txt', true); xhr.onload = function() { if (xhr.status == 200) { console.log(xhr.responseText); } } xhr.send(); } 

在这里,我提供了一些目录和结果的屏幕截图

目录 目录

最终结果 最终结果

您尝试通过ajax加载的文件为blah.txt ,而目录中a文件为blah不带扩展名,请尝试将其重命名为blah.txt或从ajax url中删除.txt扩展名:

xhr.open('GET', '/blah', true);

暂无
暂无

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

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