js使用ajax调用get接口/index

js使用ajax调用get接口/index

let http = new XMLHttpRequest();
 
    let base = "http://127.0.0.1:81/index"
 
    "http://127.0.0.1:81/index"
 
    http.open("get",base);
 
    http.send();
 
    http.onreadystatechange=function(){
 
        console.log(http.responseText);
 
    }
 
       ---------------------
 
    http.open("post",base);
 
    http.send();
 
    http.onreadystatechange=function(){
 
        console.log(http.responseText);
 
    }
 
    -------------------
 
 let base = "http://127.0.0.1:81/index"
 
    $.get(base,(user)=>{
 
        console.log(user);
 
    })
 
    $.post(base,(user)=>{
 
        console.log(user);
 
    })
const http = require("http");
 
// const server = http.createServer();
 
// server.on("request",(req,res)=>{
 
//     console.log(req.method);
 
//     res.setHeader("Content-Type","text/html;charset=utf-8");
 
//     res.end("恭喜你接口调用成功");
 
// })
 
// server.listen(80,()=>{
 
//     console.log("服务启动成功");
 
// })
 
// -----------------
 
// 解决跨域问题使用cors
 
// 1.npm i cors
 
// 2.导入require
 
// 3.使用,在app创建服务后使用
 
const express = require("express");
 
const cors = require('cors');
 
const app = express();
 
app.use(cors());
 
// app.get("url",(req,res)=>{})
 
app.get("/",(req,res)=>{
 
    res.send("get请求访问首页接口成功")
 
})
 
app.get("/index",(req,res)=>{
 
    res.send("get请求访问/index接口成功")
 
})
 
// app.post("url",(req,res)=>{})
 
app.post("/index",(req,res)=>{
 
    res.send("post请求访问/index接口成功")
 
})
 
app.listen(81,()=>{
 
    console.log("express服务启动成功");
 
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值