0
<!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

4个回答

0

已采纳
app.get("/public/example.htm",function(req,res,next){

    res.sendFile(__dirname+"/public/example.htm");
    return;
})

res.sendFile(path [, options] [, fn])

Unless the root option is set in the options object, path must be an absolute path to the file

0

跟那一样的困惑

0

app.get("public/example.htm",function(req,res){

/*res.json({
    name:'sinson',
    sex:'male'
})*/
res.sendFile("public/example.htm");
res.end();

})

改为:
app.get("/public/example.htm",function(req,res){

/*res.json({
    name:'sinson',
    sex:'male'
})*/
res.sendFile("public/example.htm");
res.end();

})

0

404就是找不到,应该是路径写错了。

撰写答案