what should I use .post vs .ajax?
what should I use .post vs .ajax?
问题:
I've always had this dilemma困境 whether to use .post or .ajax for a few situations, but I got kept using .post?
My
current project I want to be able to post the a 'wall' sort of thing,
so you write your email, name and a comment and it posts it to an
element.
Should I use .post? or .ajax? and why?
Thanks for any help!
解答:
$.post is a shorthand way of using $.ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code. $.get works on a similar principle.
$.ajax is generally better to use if you require a
greater depth of configuration over your ajax request, however if you're
only going to be using simple $.post requests in your scripts and
applications, sticking to it may be the best idea.
The only thing I would add in response to more configuration, is that if you want to be able to handle errors,
do something before sending or when the call is complete regardless of success or fail, $.post() only supports a success callback,
where $.ajax() supports beforeSend, success, error and complete callbacks. I generally prefer to always use $.ajax()
Difference between $.post and $.ajax?
JQuery 3.x
The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
var jqxhr = $.post(url, data);
// Handle results
jqxhr.done(function(result) {
//alert("ajax success");
});
jqxhr.fail(function() {
//alert("ajax error");
});
jqxhr.always(function() {
//alert("ajax complete");
});
https://api.jquery.com/jquery.post/
原文地址:https://www.cnblogs.com/chucklu/p/11549849.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 云服务器网络延迟与丢包问题定位(mtr工具)
- Spark UDF1 输入复杂结构
- Qt音视频开发6-ffmpeg解码处理
- MySQL 百万级数据量分页查询方法及其优化
- 教你设计一个超牛逼的本地缓存!
- Flutter延时任务、Flutter通过Future与Timer实现延时任务
- DDIA 笔记
- 工作流和状态机
- CentOS 6.x 搭建:Headless Chrome + ChromeDriver + Selenium的爬虫环境系统
- 聊聊dubbo-go的registryAwareCluster
- 同样是空值,null和undefined有什么异同?
- 强大到没朋友的mysql-shell及插件
- android JavaPoet记录
- JavaScript里的分号,你加还是不加?
- 技术干货 | Docker 容器逃逸案例汇集