<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery.js"></script>
</head>
<body>
<button id="get">GET</button>
<button id="post">POST</button>
<button id="fangfa">通用型方法ajax</button>
<script>
$(function () {
$("#get").click(function () {
$.get('http://127.0.0.1:8000/jquery', { a: 100, b: 200 }, function (data) {
console.log(data);
})
})
$("#post").click(function () {
$.post('http://127.0.0.1:8000/jquery', { a: 100, b: 200 }, function (data) {
console.log(data);
},'json')
})
$("#fangfa").click(function(){
$.ajax({
url:"http://127.0.0.1:8000/jquery",
data:{a:100,b:200},
dataType:'json',
type:'GET',
success:function(data){
console.log(data);
},
timeout:2000,
error:function(){
console.log("出错了");
},
})
})
})
</script>
</body>
</html>
使用node开发服务器
const express = require('express')
const app = express();
app.get('/server', (req, res) => {
res.setHeader('Access-Control-Allow-Origin','*')
res.send("asdasd")
})
app.get('/ie', (req, res) => {
res.setHeader('Access-Control-Allow-Origin','*')
res.send("htllo i111e")
})
app.post('/server',(req,res)=>{
res.setHeader('Access-Control-Allow-Origin','*')
res.send('post')
})
app.get('/delay', (req, res) => {
res.setHeader('Access-Control-Allow-Origin','*')
setTimeout(()=>{
res.send("delay")
},3000)
})
app.get('/jquery', (req, res) => {
res.setHeader('Access-Control-Allow-Origin','*')
res.send({name:"get"})
})
app.post('/jquery', (req, res) => {
res.setHeader('Access-Control-Allow-Origin','*')
res.send({name:"123post"})
})
app.listen(8000, () => {
console.log("启动成功");
})
|