好奇是否有人知道数据参数的区别.
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 的区别?
发布评论