jquery当中发送ajax请求

 

使用Express服务器

server.js里的内容:

get和post服务器

server.js里的内容:

也可写成   app.all( ' / jquery-server ' , ( request , response )=>{

                        //all 可接受所有类型的请求  get、 post、 delete等请求

                 })        

1.  jQuery发送Ajax的 get 请求

html内容

server.js里的内容:

 声明一个对象,转化成json字符串传递给浏览器

 设置【type】为 'json'  设置返回内容的格式后

返回结果是  js对象

 2.  jQuery发送Ajax的 post请求

html里的内容

server.js 里的内容

还有一个通用方法 :

$ajax({

        //请求地址: url        

        url: 'http://127.0.0.1:8000/server',

        //参数:  data  【注意data也是一个对象】

        data : {a:100 ,  b:200 },    //{ a=100, b=200}

        // 请求类型:  type

        type : 'GET',     // 'POST'

        //成功的回调函数  (data) 服务端返回的数据

        success : function (data) {

        },

        //失败的回调函数

        error : function(){

        }

})

注意:()里的是 { }  是一个对象哦,别忘了!

 

补充:

猜你喜欢

转载自blog.csdn.net/G1best/article/details/126110232