suchen

Heim  >  Fragen und Antworten  >  Hauptteil

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


高洛峰高洛峰2951 Tage vor767

Antworte allen(1)Ich werde antworten

  • 三叔

    三叔2016-11-04 11:20:37

    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


    Antwort
    0
  • StornierenAntwort