簡體   English   中英

將數組傳遞給函數-函數內部數組為空

[英]Passing array to function - array is empty inside function

我幾乎嘗試了所有事情,我有兩個代碼:

第一個,pokaz_ofe,調用正確的函數-ajaxQuery()函數中的“ generuj_oferte”,但我無法訪問ajaxQuery()中的postTab數組。 如果不刪除postData,它將使用另一個調用中的舊數組(“ pokaz_ofe”函數中的其他數組)。

function ajaxQuery(postData, func_var) {
    $.ajax({
        type: "POST",
        url: "ajax/pokaz_lst.php",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(postData),
        success: func_var
    })
    postData = [];
    delete postData;
}
function pokaz_ofe(id,tab) {
    if(!tab) {
      var postTab = [
        { "id": id }
      ]
    }
    else {
      var postTab = [
        { "id": id },
        { "listaId": tab}
      ]
    }
    ajaxQuery(postTab, generuj_oferte);
}
function generuj_oferte(res) {
    $(document).ready(function(){
        $('#modal-oferta').html( res );
    });
}

這是不同的代碼,但是我希望它更靈活:

function pokaz_ofe(id,tab) {
    if(!tab) {
      var postData = [
        { "id": id }
      ]
    }
    else {
      var postData = [
        { "id": id },
        { "listaId": tab}
      ]
    }
    $.ajax({
        type: "POST",
        url: "ajax/pokaz_ofe.php",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(postData),
        success: generuj_oferte
    })
}

這真是愚蠢的一個錯誤……現在是:

function ajaxQuery(filename, postData, func_var) {
    $.ajax({
        type: "POST",
        url: "ajax/" + filename + ".php",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(postData),
        success: func_var
    })
    postData = [];
    delete postData;
}

這全都與文件名有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM