试图覆盖全局变量时的jQuery.post()范围问题 [英] jQuery.post() scope issue while trying to overwrite global variable

查看:94
本文介绍了试图覆盖全局变量时的jQuery.post()范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与范围相关的代码中遇到了一些困难. 这是我的代码:

I'm facing some difficulties in my code related to scope. Here is my code:

<script type="text/javascript">
    var totalHoras = {};
    var dadosCategorias = {"teste": "1234"};
    var t; // timeout handler para exibição de feedback para o usuário

    $().ready(function() {
        // obtém dados das categorias
        var obterDadosCategorias = function() {
            $.post(
                "{{ baseRoute }}/cadastro/categoria/listar"
                , {
                    "ajax": "true"
                }
            ).done(function(data) {
                var obj = $.parseJSON(data);
                if (obj.result) {
                    console.log(dadosCategorias); // returns 'object{"teste": "1234"}'
                    dadosCategorias = obj.data;
                    console.log(dadosCategorias); // returns 'string(11) "it works!!!"'
                } else {
                    alert('Erro interno: não foi possível obter os dados das categorias');
                }
            });
        };

        obterDadosCategorias();
        console.log(dadosCategorias); // returns 'object{"teste": "1234"}'

问题是:为什么第三次console.log调用返回原始var值?不应该被覆盖吗?

The question is: why the hell is the third call of console.log returning the orginal var value? isn't is suposed to be overwriten?

应该是控制台

'object{"teste": "1234"}'
'string(11) "it works!!!"'
'string(11) "it works!!!"'

但是是

'object{"teste": "1234"}'
'string(11) "it works!!!"'
'object{"teste": "1234"}'

我试图在$ .ajax()函数的窗口中使用"context"选项,但也无法使用:(

I've trie to use the "context" option with window in $.ajax() function, but does not work too :(

推荐答案

谢谢大家! :D

我找到了答案.问题根本不是范围.实际上,对console.log()的调用不是按照脚本的顺序进行的.呼叫顺序以前是312,现在是123:D 我将ajax方法更改为$ .ajax()而不是$ .post(),并将选项"async"设置为false来实现此目的,就像这样:

I've found the answer. The problem wasn't the scope at all. Actually, the calls to console.log() wasn't in the order of the script. The calling order was 312 before, now it is 123 :D I changed the ajax method to $.ajax() instead of $.post() and set the option 'async' to false to achieve this, like so:

<script type="text/javascript">
    var totalHoras = {};
    var dadosCategorias = {"teste": "1234"};
    var t; // timeout handler para exibição de feedback para o usuário

    $().ready(function() {
        // obtém dados das categorias
        var obterDadosCategorias = function() {
            $.ajax(
                "{{ baseRoute }}/cadastro/categoria/listar"
                , {
                    "type": "POST"
                    , "async": false
                    , "data": {
                        "ajax": "true"
                    }
                }
            ).done(function(data) {
                var obj = $.parseJSON(data);
                if (obj.result) {
                    console.log(1, dadosCategorias);
                    dadosCategorias = obj.data;
                    console.log(2, dadosCategorias);
                } else {
                    alert('Erro interno: não foi possível obter os dados das categorias');
                }
            });
        };

        obterDadosCategorias();
        console.log(3, dadosCategorias);

这篇关于试图覆盖全局变量时的jQuery.post()范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆