ajaxを使用してjQueryでリクエストを送信する方法

記事の一般的なリストに戻る

この記事では、ajaxを使用してjQueryでリクエストを送信する方法について説明します

jquery純粋なインサイドget请求でシンプルなpost请求比較的単純な、比較的単純な
jquery里面ajax通用方法(強いのカスタムヶ月)
jquery里面ajax通用方法get请求(設定可能请求类型,参数,头信息
jquery里面ajax通用方法であるpost请求(設定可能请求类型,参数,头信息,请求体
例プレゼンテーション以下、毎日は間違いなく十分です。

リクエストを取得

$.get(url,[data],[callback],[type])
url:请求的URL路由地址
[data]:请求携带的参数
[callback]:载入成功时回调函数
[type]:设置返回内容格式

ポストリクエスト

$.post(url,[data],[callback],[type])

Maoyunの加速されたcdnでそれを入手してjquery的链接
くださいhttps://www.bootcdn.cn/

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

ケーススタディ

テスト画像ショー
内部のjQueryによるget,post,ajax方法を得る結果がサーバから返さ
ここに画像の説明を挿入しますjquery里面的ajax方法get要求が設定要求ヘッダ
ここに画像の説明を挿入します
作成1.testten文件夹及びフォルダ内に
作成2.simpel.html文件
作成3.server.js文件
テストコードであります

simpel.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 存放jquery链接  -->
    <!-- <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> -->
    <!-- 添加允许跨源属性获取链接  向该链接发送请求的时候不会发送当前域名下的cookies  一般当前域名的cookies存放着你的帐号密码-->
        <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <title>Document</title>
</head>
<body>
    <button style="background-color: tomato;">GET,非json数据格式</button>
    <button style="background-color: tomato;">GET,json格式数据返回</button>
    <button style="background-color: violet;">POST,json格式数据返回</button>
    <button style="background-color: chartreuse;">通用ajax请求</button>
    <div id="result" style="width: 180px;height: 100px;border: solid 2px teal;"></div>
    <script>
        //  jquery里面的get请求        服务端设置了字符串格式返回
        // jquery设置绑定事件 第二个按钮(下标为1的按钮),绑定事件函数为点击        $('button').eq(0).click(function(){});
        $('button').eq(0).click(function(){
     
     
            $.get('http://127.0.0.1:8000/jqueryget',{
     
     a:300,b:400},function(data){
     
     
                console.log(data);
            })
        });
        // jquery里面的get请求         服务端设置了json格式返回 前端get的请求参数也设置了json格式接收
        // jquery设置绑定事件 第一个按钮(下标为0的按钮),绑定事件函数为点击        $('button').eq(0).click(function(){});
        $('button').eq(1).click(function(){
     
     
            $.get('http://127.0.0.1:8000/jquerygetjson',{
     
     a:100,b:200},function(data){
     
     
                console.log(data);
                // 设置返回内容格式——json数据格式
                },'json')
        });
        // jquery里面的post请求   服务端设置了json格式返回 前端post的请求参数没有设置接收格式
         // jquery设置绑定事件 第二个按钮(下标为1的按钮),绑定事件函数为点击        $('button').eq(0).click(function(){});
        $('button').eq(2).click(function(){
     
     
            $.post('http://127.0.0.1:8000/jquerypostjson',{
     
     a:300,b:400},function(data){
     
     
                console.log(data);
                // 设置返回内容格式——不添加json格式,则前端显示的为字符串
                })
        });

        // 使用jquery通用方法来进行ajax请求
        $('button').eq(3).click(function(){
     
     
            $.ajax({
     
     
                // url
                url: 'http://127.0.0.1:8000/jqueryajax',
                // 参数
                data:{
     
     a:500,b:600},
                // 请求类型
                type:'GET',
                // 响应体结果格式设置   不设置则为字符串    如果设置了json格式数据接收,返回的结果不是json则会报错
                dataType: 'json',
                // 成功的回调
                success: function(data){
     
     
                console.log(data);
                },
                // 其他设置
                // 超时时间
                timeout: 3000,
                // 失败的回调,测试这个,设置后端返回的时间延时就可以了,后端设置延时1秒,这里可以设置1秒就行,必定出错成功
                error:function(){
     
     
                    console.log("出错了,超时成功。");
                },
                // 设置头信息
                headers:{
     
     qq: 999,weixin: 999}
            });
        });
    </script>
</body>

</html>

server.js文件

// 1. 引入express
const express = require('express');

// 2.创建对象
const app = express();
// 3.创建路由规则  里面的形参 request与response   (自己可以随便定义名字的)
//  建议写成  request与response  因为可以见名思意,方便自己看
// request  对请求报文的封装
// responst 对响应报文的封装
//  请求路径为'/server'

// 当使用post请求时候会因为发送的信息没有收到对应的结果所以回报错
// 所以该处使用all  表示可以接收任意类型的请求      如get post 等等


// 一:get请求
app.get('/jqueryget', (request, response)=>{
    
    
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    response.send('get请求成功,非json数据返回.');
});
// 二 :get请求json数据
app.get('/jquerygetjson', (request, response)=>{
    
    
    // 设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');
    // 响应头        *号表示所有的头信息都可以接收
    response.setHeader('Access-Control-Allow-Headers','*');
    // 响应一个数据   把这个对象返回给浏览器
    const data = {
    
    
        name: 'get请求,服务端设置了json格式返回 前端get的请求参数也设置了json格式接收'
    };
    // 对对象进行字符串转换
    let str = JSON.stringify(data);
    setTimeout(()=>{
    
    response.send(str);},1000);
});
// 三:post请求 json数据
app.post('/jquerypostjson', (request, response)=>{
    
    
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    const data = {
    
    
        name: 'post请求,服务端设置了json格式返回 但是前端post的请求参数没有设置接收格式'
    };
    let str = JSON.stringify(data);
    setTimeout(()=>{
    
    response.send(str);},1000);
});

// 四:ajax通用请求
app.all('/jqueryajax', (request, response)=>{
    
    
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    const data = {
    
    name: 'jquery的通用请求GET请求,服务端设置了json格式返回 前端的请求参数设置接收格式为json'};
    let str = JSON.stringify(data);
    setTimeout(()=>{
    
    response.send(str);},1000);
});

// 4. 监听端口启动服务
// 这里listen(8000)后面添加了一个回调,用来提示,告诉自己是否监听成功
app.listen(8000, ()=>{
    
    
    console.log("服务已经启动,8000端口监听中......");
});

その他の方法については、次のURLを参照して
くださいhttps://jquery.cuishifeng.cn/

おすすめ

転載: blog.csdn.net/weixin_47021806/article/details/112167377