0

This is my function

function getNamaKat(param1, param2) {
            data = {
                opt : param1,
                id : param2
            }
            $.ajax({
                type : 'POST',
                url :'product_list/get_nama_kat',
                dataType : 'json',
                data : data,
                success : function(hasil) {
                    return hasil.name;
                },
                error : function(hasil) {
                    alert("There is an error");
                }
            });
        }

But I always got undefined result from the function. Here'es the reult :

description:"Kategori Pakaian" id:"574f94e0f3037ddd30ad8ac4" name:"Pakaian" status:true

And i try to get the name by this code

document.getElementById("kategori_barang").innerHTML = getNamaKat("kategori", result.categoryId);

Thankyou very much for help me.

  • The reason you get undefined is because the function gets executed and is done while the call to your ajax is not yet done and still in the pipe line. You should set your success to a call back function and do the innerHTML setting there. – Saky Jun 02 '16 at 03:06
  • No-one looks at the return value of the `success` callback, and it certainly does not become the return value of the `getNamaKat` function. The only way a function can return a value is to `return` it directly within itself, not a return from some nested callback function. In this case, you can't return it because you don't have it yet. –  Jun 02 '16 at 03:30

0 Answers0