<!DOCTYPE html> <html> <head> <title>express 主页</title> <meta charset="utf-8"> <script type="text/javascript" src="jquery.min.js"></script> </head> <body> <div> I love you! </div> <button>click there to download a pictutre</button> <a href="/download">download</a> <script type="text/javascript"> $(function(){ $('button').click(function(){ $('div').load("public/example.htm",function(data,status){ console.log(data); console.log(status); console.log("success"); }) }) }) </script> </body> </html>
example.htm内容:<img src="example.jpg" alt="ajax 加载的图片">
后端代码
var express = require("express"); var app = express(); app.get("public/example.htm",function(req,res){ /*res.json({ name:'sinson', sex:'male' })*/ res.sendFile("public/example.htm"); res.end(); }) 但是结果是Failed to load resource: the server responded with a status of 404 (Not Found) Cannot GET /public/example.htm
res.sendFile(path [, options] [, fn])
Unless the root option is set in the options object, path must be an absolute path to the file