博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js进阶 14-4 $.get()方法和$.post()方法如何使用
阅读量:4620 次
发布时间:2019-06-09

本文共 2120 字,大约阅读时间需要 7 分钟。

js进阶 14-4 $.get()方法和$.post()方法如何使用

一、总结

一句话总结:$.get(URL,callback); $.post(URL,data,callback); callback函数和load()方法里面的callback一样。

 

1、load方法和$.get()以及$.post()方法的区别是什么(load也可以实现ajax的post和get请求)?

load方法是局部变量,前面需要加上监听对象,监听对象就是返回结果放置的元素

$.get()以及$.post()时全局方法,不必加上监听对象

20             // $('#test').load('test.php?password=1234560')

 

40 $.get('testGet.php',{password:'123456'},function(responseTxt,statusTxt){ 41 // alert(responseTxt) 42 $('#test').html('responseTxt:'+responseTxt+'
'+'status: '+statusTxt) 43 })

 

 

2、$.get()提交数据四种方法?

a、url中?接参数

b、字符串(jquery1.3之后支持)

c、json对象

 

20                   //get方式提交数据121               /*22 $.get('test.html',function(data,statusTxt){ 23 alert(data) 24 alert(statusTxt) 25 }) 26 27 //get方式提交数据2 28 $.get('testGet.php?password=123456',function(responseTxt,statusTxt){ 29 // alert(responseTxt) 30 $('#test').html('responseTxt:'+responseTxt+'
'+'status: '+statusTxt) 31 }) 32 33 //get方式提交数据3 34 $.get('testGet.php','password=123456',function(responseTxt,statusTxt){ 35 // alert(responseTxt) 36 $('#test').html('responseTxt:'+responseTxt+'
'+'status: '+statusTxt) 37 }) 38 39 //get方式提交数据4 40 $.get('testGet.php',{password:'123456'},function(responseTxt,statusTxt){ 41 // alert(responseTxt) 42 $('#test').html('responseTxt:'+responseTxt+'
'+'status: '+statusTxt) 43 }) 44 45 //post方式提交数据1 46 $.post('testPost.php',{password:'123456'},function(responseTxt,statusTxt){ 47 // alert(responseTxt) 48 $('#test').html('responseTxt:'+responseTxt+'
'+'status: '+statusTxt) 49 })

 

 

二、$.get()方法和$.post()方法如何使用

1、相关知识

get()和post()方法

两种在客户端和服务器端进行请求-响应的常用方法是:GET和POST.

GET基本上用于从服务器获得(取回)数据。注释:GET方法可能返回缓存数据。
POST也可用于从服务器获取数据。不过,POST方法不会缓存数据,并且常用于连同请求一起发送数据。

  • $.get(URL,callback);

    参数

    1. 第一个参数是我们希望请求的URL;
    2. 第二个参数是回调函数。第一个回调参数存有被请求页面的内容,第二个回调参数存有请求的状态。
  • $.post(URL,data,callback);

    参数

    1. 必需的URL参数规定您希望请求的URL。
    2. 可选的data参数规定连同请求发送的数据
    3. 可选的callback参数是请求成功后所执行的函数名。第一个回调参数存有被请求页面的内容,而第二个参数存有请求的状态
    4. type:返回内容格式,xml,html,script,json,text,_default。
 

 

2、代码

 

html

1  2  3  5  6     
7 演示文档 8 9 11 12 13 14 15
16 60 61

 

php(post请求和get请求)

get

1 

post

1 

 

 

 

 

 

转载于:https://www.cnblogs.com/Renyi-Fan/p/9340045.html

你可能感兴趣的文章
测试开发学习进阶教程 视频&PDF
查看>>
C#基础-连接Access与SQL Server
查看>>
autofac
查看>>
MacOS 系统终端上传文件到 linux 服务器
查看>>
Excel导出POI
查看>>
兼容性
查看>>
自动执行sftp命令的脚本
查看>>
转 Merkle Tree(默克尔树)算法解析
查看>>
网络编程基础之socket编程
查看>>
各种浏览器的user-agent和
查看>>
Restful levels
查看>>
Phonegap移动开发:布局总结(一) 全局
查看>>
Java 变参函数的实现
查看>>
nrf51 SDK自带例程的解读
查看>>
SESSION技术
查看>>
数据结构(五)之直接插入排序
查看>>
SQL函数——LENGTH()和LENGTHB()
查看>>
vim - manual -个人笔记
查看>>
详解Javascript中prototype属性(推荐)
查看>>
angularjs实现首页轮播图
查看>>