HTML 页面用jquery异步请求数据失败 后台用node.js处理

<!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.2k
4 个回答
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

跟那一样的困惑

新手上路,请多包涵

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();

})

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题