【问题标题】:How to pass success function name as a function parameter to AJAX call?如何将成功函数名称作为函数参数传递给 AJAX 调用?
【发布时间】:2018-07-23 11:52:46
【问题描述】:

这里我试图将 AJAX 调用作为单个函数进行,因为我将成功函数名称作为函数参数传递给 AJAX 调用函数。

我尝试了以下函数:

function ApiCallFunction(Datatext, ApiName, FunctionName) {
  $.ajax({
    url: Apiurl + ApiName,
    type: "POST",
    data: Datatext,
    contentType: "application/json",
    dataType: "json",
    success: function(data) {
      var funname = FunctionName + '("' + data + '")';
      eval(funname);
    },
    error: function(error) {
      jsonValue = jQuery.parseJSON(error.responseText);
      ErrorWhileSave(jsonValue.Message);
    },
    failure: function(response) {
      ErrorWhileSave("");
    }
  });
}

函数调用:

var datatext = {
  BillChild: {},
  BillDate: "2018-07-23T08:35:32.319Z",
  EntryTime: "2018-07-23T08:35:32.319Z",
  ExitTime: "2018-07-23T08:35:32.319Z",
  TotalTime: "2018-07-23T08:35:32.319Z",
  Total: 0,
  OtherCharges: 0,
  Discount: 0,
  TaxableAmount: 0,
  TotalTax: 0,
  GrandTotal: 0,
  RoundOff: 0,
  NetAmount: 0,
  ByCash: 0,
  ByBank: 0,
  CashReceived: 0,
  BalanceReceivable: 0,
  FirmId: 0,
  UserId: 0,
  BillId: 35,
  CustomerId: 0,
  BranchId: 0,
  BillType: "string",
  BillNo: "string",
  PaymentType: "string",
  Notes: "string",
  TaxType: "string",
  BankId: "string",
  CreatedBy: "string",
  HostIp: "string",
  BranchTransfer: "string",
  ConsultId: 0,
  SearchKey: "string",
  Flag: "SELECTONE"
};

var Datatext = (JSON.stringify(datatext));
ApiCallFunction(Datatext, "Bill_master", "ReturnFunction");

我尝试使用的 Success 函数是:

function ReturnFunction(ReturnValue) {
  alert(data.data.Table1[0].BillId);
}

当我尝试alert(ReturnValue) 时,它显示为object object。我也试过ReturnValue.data.data.Table1[0].BillId 仍然无法使用这些值。 AJAX 调用成功,我从中获得了价值,但我无法将结果 JSON 对象传递给其他函数。

如何将 JSON 对象传递给其他函数?请帮帮我。

【问题讨论】:

  • success:FunctionName.......?
  • 可以直接传函数,在ajax回调中调用
  • @vikscool 我也试过了!! FunctionName 现在作为变量工作!!

标签: javascript c# jquery asp.net ajax


【解决方案1】:

您可以通过不同的方式来实现您想要做的事情。

方法一简单地将函数对象作为参数赋值

function ApiCallFunction(Datatext, ApiName, onSucess,onError) {
    $.ajax({
        url: Apiurl + ApiName,
        type: "POST",
        data: Datatext,
        contentType: "application/json",
        dataType: "json",
        success: onSucess,
        error: onError,
        failure: function (response) {
            ErrorWhileSave("");
        }
    });
}

并将函数实现为:

function ReturnFunction(response){
 //assuming that response is of JSON type
 alert(response.data.Table1[0].BillId);
}

function myError(response){
 console.log(JSON.parse(response.responseText).Message);
}

调用:

ApiCallFunction(DataText,"Bill_master",ReturnFunction,myError);

方法2如果你碰巧有一个字符串而不是函数对象

function ApiCallFunction(Datatext, ApiName, FunctionName) {
    $.ajax({
        url: Apiurl + ApiName,
        type: "POST",
        data: Datatext,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
             window[FunctionName].apply(this,data);
        },
        error: function (error) {
            jsonValue = jQuery.parseJSON(error.responseText);
            ErrorWhileSave(jsonValue.Message);
        },
        failure: function (response) {
            ErrorWhileSave("");
        }
    });
}

调用:

ApiCallFunction(DataText,"Bill_master","ReturnFunction");

【讨论】:

  • 附加检查可能有助于防止出错! typeof callback === "function" (函数作为引用) typeof window["callback"] === "function" (函数名作为字符串传递)
  • @jeetaz 是的,这可能会有所帮助。
  • success: onSucess 不起作用,因为我们需要 onSucess 变量上的函数名称。不幸的是,它也不能被识别为这种格式的变量
  • @kirk 是否有任何错误,您是否尝试过按照我提到的方式调用它。
  • Cannot read property 'data' of undefined 是我得到的错误
【解决方案2】:

可以传递函数地址(回调):

function ApiCallFunction(Datatext, ApiName, FunctionName) {
        $.ajax({
            url: Apiurl + ApiName,
            type: "POST",
            data: Datatext,
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                FunctionName(data);
            },
            error: function (error) {
                jsonValue = jQuery.parseJSON(error.responseText);
                ErrorWhileSave(jsonValue.Message);
            },
            failure: function (response) {
                ErrorWhileSave("");
            }
        });
    }

function ReturnFunction(data) {
   alert(data.data.Table1[0].BillId);
}

ApiCallFunction(Datatext, "Bill_master", ReturnFunction);

【讨论】:

    【解决方案3】:

    在 ReturnFunction 中,我认为数据未定义,也许这应该起作用:

    function ReturnFunction(ReturnValue) {
      alert(ReturnValue.data.Table1[0].BillId);
    }
    

    【讨论】:

    • 感谢您的帮助,!!我已经尝试过了,但警报不起作用
    • console.log(ReturnValue) 的输出是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    相关资源
    最近更新 更多