亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將成功函數名稱作為函數參數傳遞給 AJAX 調用?

如何將成功函數名稱作為函數參數傳遞給 AJAX 調用?

C#
開心每一天1111 2021-10-23 17:11:12
在這里,我試圖將 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 對象傳遞給其他函數?請幫我。
查看完整描述

1 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

您可以通過不同的方式實現您的目標。


方法一簡單地將函數對象作為參數賦值


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");


查看完整回答
反對 回復 2021-10-23
  • 1 回答
  • 0 關注
  • 172 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號