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

查看:104
本文介绍了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

解决方案

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

这篇关于HTML 页面用jquery异步请求数据失败 后台用node.js处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆