douqie1816 2016-11-07 22:34
浏览 56

从jquery成功传递变量

I posted a similar question, but since stack overflows go dead real quick, I feel like I didn't get an answer.

Sometimes I don't understand things unless it's done to my code. People tend to explain things really awfully. Example: How do I return the response from an asynchronous call?

In that first answer, he explains how to restructure the code. However he doesn't explain the steps correctly. He doesn't explain what callback is or what result is. He just throws them in there. I'm assuming he's giving multiple examples on how to do it, but he isn't explaining what to use.

The following is my code that I had originally:

var admin_data = false;
function getPlayerAdmin(admin_data){
    var formData = {
        'player_id' : $('#post-v').attr('data-val')
    };
    $.ajax({
        type: 'post',
        url: '/popins/player/controls',
        data : formData,
        dataType : 'json',
        success: function(admin_data_vals, admin_data) {
            if(admin_data_vals.controls.is_admin == true){
                admin_data = true;
            }else{
                admin_data = false;
            }
        },
        error : function(jqXHR, textStatus, errorThrown){
            console.log(jqXHR);
        }
    });
}

getPlayerAdmin();
alert(admin_data);

The following is what I tried to restructure based on his... example:

var admin_data = false;

var result = getPlayerAdmin();

getPlayerAdmin(function(result) {
    if(admin_data_vals.controls.is_admin == true){
        admin_data = true;
    }else{
        admin_data = false;
    }
});

function getPlayerAdmin(callback){
    var formData = {
        'player_id' : $('#post-v').attr('data-val')
    };
    $.ajax({
        type: 'post',
        url: '/popins/player/controls',
        data : formData,
        dataType : 'json',
        success: function(admin_data_vals, callback) {

        },
        error : function(jqXHR, textStatus, errorThrown){
            console.log(jqXHR);
        }
    });
}

getPlayerAdmin();
alert(admin_data);

I'm trying to get the variable out of here so that I can use it in another function.

I'm so confused, help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duangu1868 2016-11-08 02:49
    关注

    You have to understand that some people are at school or work, and not all the day on SO answering people's questions, that could furthermore be easily answered by google-ing and studying the documentation.

    Anyway, here's a fully commented piece of code:

    function getData(callback) { // You pass a callback function as a parameter
        var formData = {
            'player_id' : $('#post-v').attr('data-val')
        };
        $.ajax({
            type: 'post',
            url: '/popins/player/controls',
            data : formData,
            dataType : 'json', // data passed in the success function will going to be parsed if you specify the `dataType`.
            success: function(data) {
              // on success, you call the callback function, passing the data you just got
              callback(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
              // on error, just log the error.
              console.log(errorThrown);
            }
        });
    }
    
    getData(function(data) { // You are calling the `getData` function, passing another function as a parameter, which is going to get called on success
    
        // This will log the object that you asked your server for.
        console.log(data); 
    });
    

    The function getData queries the server for data and then calls the function that you are passing as a parameter, in this case an anonymous function. You could easily just call getData like this, doing basically one more step:

    function analyseData(data) { 
        console.log(data); 
    }
    getData(analyseData);
    

    Now if you have any more questions, please ask them, but be respectful. SO people are not your employees or teachers here to explain you how this and this works. You ought to do some research by yourself. On this note, have a nice evening!

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭