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. [LeetCode] 148. 排序链表 ☆☆☆(归并排序)

    148.排序链表 描述 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3输出: 1->2->3-> ...

  2. lumen添加自定义异常

    在公用工具类写异常类 <?php namespace Brady\Tool\Exception; use Brady\Tool\Constant\ErrorMsg; use \Exception ...

  3. cmd中for的用法

    在cmd窗口输入for /?后的原文,被我自己“翻译”了一下,更像人话了. 推荐去https://www.cnblogs.com/cbugs/p/8992059.html这篇部落格里去看看,讲的更好. ...

  4. 说说客户端访问一个链接URL的全过程

    讲讲登录权限是如何控制的   我们可以把这个过程类比成一个电话对话的过程.当我们要打电话给某个人,首先要知道对方的电话号码,然后进行拨号.打通电话后我们会进行对话,当然要对话肯定需要共同的语言,如果一 ...

  5. Vue + Springboot 开发的简单的用户管理系统

    后台接口如下: 页面如下: 1. 主页,显示所有的用户信息 2. 点击详情,看到某个id的用户的详细信息 3. 点击编辑按钮,跳转到的详细的编辑(更新)页面 4. 添加用户页面 对应的vue代码如下 ...

  6. 多个ip地址匹配正则表达式

    匹配规则:多个ip地址使用,号进行分割 例如:1.1.1.1,2.2.2.2var iplist =/^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}( ...

  7. JS控制SVG缩放+鼠标控制事件

    话不多说,直接上代码吧,不行你砍我... <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  8. .Net系列 Transaction 事务

    Transactions 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit).在计算机术语中,事务通常就是指数 ...

  9. CORS通信

    CORS 是一个 W3C 标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨域的服务器,发出XMLHttpRequest请 ...

  10. Mybatis面向接口式编程

    Mybatis面向接口编程 1.xml文件书写格式 <?xml version="1.0" encoding="UTF-8" ?> <!DOC ...