Express运用AJAX及实现JSONP

一、get请求:

index.html

<html>
<head>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>
<input type="button" value="Ajax" id="get-data"/>

<script type="text/javascript">
$(document).ready(function () {
    $('#get-data').click(function (e) {
        $.ajax({
            url:'/get_data',
            type:'get',
            data:{},
            success:function (data) {
				console.log(data);
            },
            error:function (err) {

            }
        });
    });
});
</script>
</body>
</html>

test.js

const express = require("express");
const app = express();

//定义一个存放静态资源的目录
app.use('/src', express.static("src"));

app.get("/index.html", function(req, res, next){
	res.sendFile( __dirname + "/" + "index.html" );
})

app.get("/get_data", function(req, res, next){
    var obj = {
            title:'耶鲁留学生的2000张自拍照',
            src:'腾讯新闻',
            content:'推动新一代人工智能健康发展 更好造福世界各国人民'
        }
   res.json(obj);
})

const server = app.listen(8080, function(){
	console.log("启动成功!");
})

二、post请求:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>

<body>
<form action="/get_data" method="POST">
    First Name: <input type="text" id="first-name">  <br>

    Last Name: <input type="text" id="last-name">
    <input type="button" value="提交" id="get-data"/>
</form>

<script>
$(function(){
    $("#get-data").click(function(){
        $.ajax({
            type: "POST",
            url: '/get_data',
            data: {'firstName': $("#first-name").val(), 'lastName': $("#last-name").val()},
            dataType: "json",
            success: function (data) {
                console.log(data);
            },
            error: function (data) {
                console.log(data);
            }
        })
    })
})
</script>
</body>
</html>

index.js:

let express = require("express");
let app = express();
let bodyParser = require("body-parser");

let urlencodedParser = bodyParser.urlencoded({extended: false});

app.get("/index", function(req, res, next){
    res.sendFile(__dirname + "/views/" + "index.html")
})

app.post("/get_data", urlencodedParser, function(req, res, next){
    let response = {
        first_name: req.body.firstName,
        last_name: req.body.lastName
    }

    res.type("application/json");
    res.jsonp(response);
});

app.listen(8080, function(){
    console.log("开始请求");
})

三、JSONP(只有get方法支持):

8080端口:

let express = require("express");
let app = express();

app.use("/public", express.static("public"));

app.get("/index", function(req, res, next){
    res.sendFile(__dirname + "/" + "views/index.html");
});

app.get("/get_data", function(req, res, next){
    let response = {
        "title": "耶鲁留学生的2000张自拍照",
        "content": "耶鲁留学生的2000张自拍照,耶鲁留学生的2000张自拍照,耶鲁留学生的2000张自拍照,耶鲁留学生的2000张自拍照,耶鲁留学生的2000张自拍照"
    };
    res.type('application/json');
    res.jsonp(response);
});

app.listen(8080, function(){
    console.log("开始执行请求");
});

8000端口:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>

<input type="button" value="获取数据" id="get-data"/>

<script>
    $(function(){
        $("#get-data").click(function(){
            $.ajax({
                type: "GET",
                url: 'http://127.0.0.1:8080/get_data',
                data: {},
                dataType: "jsonp",
                success: function(data){
                    console.log(data);
                },
                error: function(data){

                }
            })
        })
    })
</script>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值