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/

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});

what should I use .post vs .ajax?的更多相关文章

  1. jQuery之ajax实现篇

    jQuery的ajax方法非常好用,这么好的东西,你想拥有一个属于自己的ajax么?接下来,我们来自己做一个简单的ajax吧. 实现功能 由于jq中的ajax方法是用了内置的deferred模块,是P ...

  2. Ajax及跨域

    概念 Ajax Ajax,Asynchronous JavaScript and XML,字面意思:异步的 JavaScript 和 XML,是指一种创建交互式网页应用的网页开发技术. 用于异步地去获 ...

  3. 一个粗心的Bug,JSON格式不规范导致AJAX错误

    一.事件回放  今天工作时碰到了一个奇怪的问题,这个问题很早很早以前也碰到过,不过没想到过这么久了竟然又栽在这里. 当时正在联调一个项目,由于后端没有提供数据接口,于是我直接本地建立了一个 json ...

  4. ABP文档 - Javascript Api - AJAX

    本节内容: AJAX操作相关问题 ABP的方式 AJAX 返回信息 处理错误 HTTP 状态码 WrapResult和DontWrapResult特性 Asp.net Mvc 控制器 Asp.net ...

  5. ajax异步请求

    做前端开发的朋友对于ajax异步更新一定印象深刻,作为刚入坑的小白,今天就和大家一起聊聊关于ajax异步请求的那点事.既然是ajax就少不了jQuery的知识,推荐大家访问www.w3school.c ...

  6. 调用AJAX做登陆和注册

    先建立一个页面来检测一下我们建立的用户名能不能用,看一下有没有已经存在的用户名吗 可以通过ajax提示一下 $("#uid").blur(function(){ //取用户名 va ...

  7. Ajax 概念 分析 举例

    Ajax是结合了访问数据库,数据访问,Jquery 可以做页面局部刷新或者说是页面不刷新,我可以让页面不刷新,仅仅是数据的刷新,没有频繁的刷页面,是现在比较常用的一种方式做页面那么它是怎么实现页面无刷 ...

  8. ajax

    常见的HTTP状态码状态码:200 请求成功.一般用于GET和POST方法 OK301 资源移动.所请求资源移动到新的URL,浏览器自动跳转到新的URL Moved Permanently304 未修 ...

  9. 学习笔记之MVC级联及Ajax操作

    由于刚转型到MVC,MVC的架构模式很多不是很清楚,比如今天就想做个级联的操作,因为之前的ASP.NET的方式是通过:控件-->添加事件-->后台编写级联事件进行触发,但是这个MVC就不同 ...

  10. javascript表单的Ajax 提交插件的使用

    Ajax 提交插件 form.js 表单的下载地址:官方网站:http://malsup.com/jquery/form/ form.js 插件有两个核心方法:ajaxForm()和ajaxSubmi ...

随机推荐

  1. 关于get 和post 方法的比较

    地址:https://my.oschina.net/leejun2005/blog/136820 点击这里

  2. Windows Server 2008上网设置——IE

    IE安装设置 在 Windows Sever 2008 中打开 IE 浏览器时,IE 会出现[已启用 Internet Explorer 增强的安全配置]的提示信息. Windows Server 2 ...

  3. 记一次oracle 11g数据导入

    1.ORACLE数据库数据导入到测试库环境 172.15.1.51 root  kic@test 172.15.1.52 root 一般先将数据导入52的环境(配置比较低),再将数据导入51的环境(本 ...

  4. frp服务搭建

    安装命令 C wget --no-check-certificate https://raw.githubusercontent.com/clangcn/onekey-install-shell/ma ...

  5. java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable

    [转]https://stackoverflow.com/questions/53539930/java-lang-abstractmethoderror-org-powermock-api-mock ...

  6. LESS简介与使用方法

    less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. 一.传统写法与 less 写法对比 1. 传统 css 写法: .con ...

  7. eclipse cdt 代码对齐

    ctrl + shift + F 快速对齐 平时对小节太不重视了 .

  8. Java 中待总结工具类学习(自定义注解,读取配置,字面List)

    1.使用 MessageFormat 格式化文本 int planet = 7; String event = "a disturbance in the Force"; Stri ...

  9. 云计算(1)-为什么要使用cloud

    云计算-为什么要使用cloud Many cloud providers(一些提供云服务的商家) EC2:提供computing services S3:提供the ability to store ...

  10. 分享一个快的飞起的maven的settings.xml文件

    <?xml version="1.0"?> <settings> <localRepository>/home/yizhen/.m2/repos ...