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服务启动成功");
})