Javascript jQuery Ajax 成功函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8971381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:15:55  来源:igfitidea点击:

jQuery Ajax success function parameters

javascriptjqueryajax

提问by nullException

  $.ajax({  
            type: "POST",  
            url: "contacts.php",  
            data: dataString,  
            cache: false,  
            success: function(data, status, settings)  
            {  
               alert(The request URL and DATA);
            }  
            ,
            error: function(ajaxrequest, ajaxOptions, thrownError)  
            {  

            }  
        });

How can I alert the The request URL and DATA parameters inside the Success function?

如何提醒 Success 函数中的 The request URL 和 DATA 参数?

Thank You

谢谢你

回答by Alex K.

You can simply;

你可以简单地;

success: function(data, textStatus, jqXHR)
{
   alert(this.data + "," + this.url); 
}

回答by snostorm

Adapted from Alex K.'s answer, but using console.log instead:

改编自 Alex K. 的回答,但使用 console.log 代替:

success: function(data, textStatus, jqXHR)
{
   console.log(this.data + "," + this.url); 
}

This will output the data to the debugging console instead of a modal dialog.

这会将数据输出到调试控制台而不是模式对话框。

回答by ricsdeol

I needed return some data in sucess response like:

我需要在成功响应中返回一些数据,例如:

Action (Rais):

动作(Rais):

  def comment
    comnent = AlarmComment.new alarm_id: params[:id],
                user_id:  current_user.id, comment: params[:comment]

    if comnent.save
      render json: comnent, status: :created
    else
      head status: :unprocessable_entity
    end
  end

My Ajax (Coffee)

我的阿贾克斯(咖啡)

  $.ajax(
    url: "/alarms/#{alarm_id}/comment/"
    dataType: "json"
    method: "POST",
    data:
      comment: user_comment
  ).done( ->
    alert 'Comentário adicionado com sucesso'
  ).fail ->
    alert 'Erro ao adicionar'