【问题标题】:how to use beforeSend option in $.post()如何在 $.post() 中使用 beforeSend 选项
【发布时间】:2016-01-03 07:39:51
【问题描述】:

我正在使用 ajax,我在 It 中使用了 beforeSend 选项。但我想知道如何使用 $.post() 中的 beforeSend 选项 ...

我的 ajax 代码:

$.ajax({
    type: 'POST',
    url: url,
    data: data,
    beforeSend: function () {
        //some condition 
    },
    success: function (){},
    error: function () {}
});

在 $.post() 中:

$.post(url, data, function())

但是什么时候添加 beforeSend 选项

【问题讨论】:

标签: jquery ajax html


【解决方案1】:

比起嵌入嵌套函数调用,我更喜欢将它们作为单独的函数。这里我使用 BeforeSend 来显示一个动画图标。当函数完成时,我隐藏图标:

function BeforeSend(jqXHR, settings )
{
    $("#WorkingGif").show();
}

如果你是问在 ajax 调用中参数的顺序应该是什么,那没关系。

我是这样使用它的:

$.ajax({
    url:"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2248907%22&format=json",
    type:"POST",
    beforeSend:BeforeSend,
    complete:OnComplete,
    success:OnSuccess,
    error:OnError
}); 

【讨论】:

    【解决方案2】:

    如您所见here,$.post 似乎没有beforeSend 功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多