Data from post request

后端 未结 3 617
面向向阳花
面向向阳花 2021-01-24 01:29
var pload = function(ctrl, func){
    var dataa;
    $.post(\"/index.php/\"+ctrl+\"/\"+func,{}, function(data){
    dataa = data;
    });
    return dataa;
};

var bind          


        
相关标签:
3条回答
  • 2021-01-24 02:01

    Try using callback.

    function pload(ctrl, func,callback){
        $.post("/index.php/"+ctrl+"/"+func,{}, function(data){
            callback(data);
        });
    };
    
    var bind = function(hashtag, ctrl, func, div){
        $(document).on("click", "a[href="+hashtag+"]", function() {
            pload(ctrl, func,function(body){
                alert(body);
                $(div).html(body);
            });             
        })
    }
    
    0 讨论(0)
  • 2021-01-24 02:10

    You're not getting any value back because $.post is asynchronous, and won't halt the program until it receives a value back.

    You should move the data handling portion to the function which will be called when the async call returns its result.

    var pload = function(ctrl, func){
        var dataa;
        $.post("/index.php/"+ctrl+"/"+func,
          {}, 
          //this function will be called when $.post receives its response
          function(data){
            alert(data);
            $(div).html(data);
          });
        return dataa;
    };
    
    0 讨论(0)
  • 2021-01-24 02:16

    tyr this:

    var pload = function(ctrl, func){
        var dataa;
        $.post("/index.php/"+ctrl+"/"+func,{}, function(data){
        dataa = data;
        return dataa;
        });
    
    };
    
    0 讨论(0)
提交回复
热议问题