$.post 和 $.ajax 的区别?

编程入门 行业动态 更新时间:2024-10-12 05:55:21
本文介绍了$.post 和 $.ajax 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好奇是否有人知道数据参数的区别.

Curious if anyone knows what the difference is in regards to the data parameter.

我有一个 $.post 方法,它采用 $('#myform').serialize() 作为我的数据参数并且有效.

I have a $.post method that takes a $('#myform').serialize() as my data param and works.

如果我使用 $.ajax() 方法尝试相同的方法,它不起作用,因为我的数据参数看起来不正确.

If I try the same using the $.ajax() approach, it doesn't work as my data param doesn't appear correct.

有谁知道区别以及我可能会使用什么来代替上面的 .serialize?

Does anyone know the difference and what I might use instead of the above .serialize?

推荐答案

在重新阅读一些在线文档后,我决定坚持使用 $.post 而不是 $.ajax.

After re-reading some online documentation, I decided to stick with $.post over $.ajax.

$.ajax 方法的数据参数与 $.post 方法不同,不确定具体是什么,但有区别.

The $.ajax method's data param does something different than the $.post method does, not sure what exactly, but there is a difference.

我想使用 $.ajax 的唯一原因是因为我希望能够处理事件,但没有意识到我可以使用 $.post 来做到这一点.

The only reason I wanted to use $.ajax is because I wanted to be able to handle events and didn't realize I could do so with $.post.

这是我的结局

function GetSearchItems() { var url = '@Url.Action("GetShopSearchResults", "Shop", New With {.area = "Shop"})'; var data = $("#ShopPane").serialize(); // Clear container $('#shopResultsContainer').html(''); // Retrieve data from action method var jqxhr = $.post(url, data); // Handle results jqxhr.success(function(result) { //alert("ajax success"); $('#shopResultsContainer').html(result.ViewMarkup); }); jqxhr.error(function() { //alert("ajax error"); }); jqxhrplete(function() { //alert("ajax complete"); }); // Show results container $("#shopResultsContainer").slideDown('slow'); }

JQuery 3.x

jqXHR.success()、jqXHR.error() 和 jqXHRplete() 回调从 jQuery 3.0 开始删除方法.您可以使用 jqXHR.done(),jqXHR.fail() 和 jqXHR.always() 代替.

The jqXHR.success(), jqXHR.error(), and jqXHRplete() 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"); });

api.jquery/jquery.post/

更多推荐

$.post 和 $.ajax 的区别?

本文发布于:2023-10-10 21:26:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   post   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!