Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有VARS函数的Javascript/jquery AJAX post返回未定义的_Javascript_Jquery_Json_Ajax - Fatal编程技术网

带有VARS函数的Javascript/jquery AJAX post返回未定义的

带有VARS函数的Javascript/jquery AJAX post返回未定义的,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我创建此同步Ajax函数只是为了发布数据: function postme(id, tt) { tt['id'] = id; $.ajax({ async:false, type: 'post', url: 'post.php', data: tt, success: function(response){ console.log(response);return response; } }); } 响应是:{“Info”:“aoaaa”} console.log(响应)在

我创建此同步Ajax函数只是为了发布数据:

function postme(id, tt) {
 tt['id'] = id;

 $.ajax({
   async:false, type: 'post', url: 'post.php', data: tt,
   success: function(response){ console.log(response);return response; }
 });
}
响应是:
{“Info”:“aoaaa”}

console.log(响应)
在控制台中提供JSON,但返回值为“undefined”。我试着这样称呼它:

postme('generate', {'1':'ee','w':'yy'});
function postme(id, tt) {
    tt['id'] = id;

    return $.ajax({
        async: true,
        type: 'post',
        url: 'post.php',
        data: tt
    });
}

postme(...).then(function(response) {
    // process response here
}, function (err) {
    // handle error here
});
我尝试了JSON解析,给这个函数取了一个名字,我尝试了我在网上找到的所有东西,但它就是不起作用

我已经看过了,但这似乎对我不起作用,因为函数不发送像我一样的变量,它只会执行
foo()
postme()在我的情况下。我该如何使用VAR


编辑:我不需要使用异步,如果有更好的代码不使用它,我宁愿使用它。

首先,停止使用同步Ajax。这对用户体验来说是可怕的,应该永远不要使用

但是,如果您使用的是同步Ajax,那么问题在于
posme()
没有返回值。您将在
success
处理程序中返回,但这将返回到ajax基础结构中,而不是返回到
posme()
的调用方。相反,您可以简单地执行以下操作:

function postme(id, tt) {
    tt['id'] = id;
    var retVal;

    $.ajax({
        async: false,
        type: 'post',
        url: 'post.php',
        data: tt,
        success: function (response) {
            console.log(response);
            retVal = response;
        }
    });
    return retVal;
}

您真正应该做的是像这样使用异步ajax:

postme('generate', {'1':'ee','w':'yy'});
function postme(id, tt) {
    tt['id'] = id;

    return $.ajax({
        async: true,
        type: 'post',
        url: 'post.php',
        data: tt
    });
}

postme(...).then(function(response) {
    // process response here
}, function (err) {
    // handle error here
});

首先,停止使用同步Ajax。这对用户体验来说是可怕的,应该永远不要使用

但是,如果您使用的是同步Ajax,那么问题在于
posme()
没有返回值。您将在
success
处理程序中返回,但这将返回到ajax基础结构中,而不是返回到
posme()
的调用方。相反,您可以简单地执行以下操作:

function postme(id, tt) {
    tt['id'] = id;
    var retVal;

    $.ajax({
        async: false,
        type: 'post',
        url: 'post.php',
        data: tt,
        success: function (response) {
            console.log(response);
            retVal = response;
        }
    });
    return retVal;
}

您真正应该做的是像这样使用异步ajax:

postme('generate', {'1':'ee','w':'yy'});
function postme(id, tt) {
    tt['id'] = id;

    return $.ajax({
        async: true,
        type: 'post',
        url: 'post.php',
        data: tt
    });
}

postme(...).then(function(response) {
    // process response here
}, function (err) {
    // handle error here
});

我同意应该避免使用同步AJAX,但如果您必须这样做,则应该可以:

function postme(id, tt) {
 var r = null;
 tt['id'] = id;

 $.ajax({
   async:false, type: 'post', url: 'post.php', data: tt,
   success: function(response){ console.log(response);r =response; }
 });

 return r;
}

我同意应该避免使用同步AJAX,但如果您必须这样做,则应该可以:

function postme(id, tt) {
 var r = null;
 tt['id'] = id;

 $.ajax({
   async:false, type: 'post', url: 'post.php', data: tt,
   success: function(response){ console.log(response);r =response; }
 });

 return r;
}

假设在某些函数中使用了返回值,例如:

function foo(response) { /* some code */}
然后提供foo作为回调

function postme(id, tt, callback) {
 tt['id'] = id;

 $.ajax({
   type: 'post', url: 'post.php', data: tt,
   success: callback
 });
}
将函数“postme”称为:

postme(param1, param2, foo)

假设在某些函数中使用了返回值,例如:

function foo(response) { /* some code */}
然后提供foo作为回调

function postme(id, tt, callback) {
 tt['id'] = id;

 $.ajax({
   type: 'post', url: 'post.php', data: tt,
   success: callback
 });
}
将函数“postme”称为:

postme(param1, param2, foo)

@jfriend00这不是重复的,请阅读我的问题。哦,天哪。你在做同步Ajax。那太可怕了。我将取消标记为dup,但这是你的第一个问题。停止使用同步Ajax。这对用户体验来说太可怕了。你真的能读懂每缩进一个空格的代码吗?我当然不能。@jfriend00这不是重复的。请阅读我的问题。哦,天哪。你在做同步Ajax。那太可怕了。我将取消标记为dup,但这是你的第一个问题。停止使用同步Ajax。这对用户体验来说太可怕了。你真的能读懂每缩进一个空格的代码吗?我当然不能。编辑:我不需要使用异步,如果有更好的代码不使用它,我宁愿使用它。编辑:我不需要使用异步,如果有更好的代码不使用它,我宁愿使用它。编辑:我不需要使用异步,如果有更好的代码不使用它,我宁愿使用它。如果你不需要同步AJAX,那么它就是一个复制品:。上面的jfriend00答案有一个很好的例子说明了如何使用async实现这一点。请注意,浏览器已经弃用了
async:false
,现在在控制台中警告使用时会弃用它。注意:我不需要使用async,如果有更好的代码不使用它,我宁愿使用它。如果不需要同步AJAX,那么它就是一个复制品:。上面的jfriend00答案提供了一个很好的例子,说明了如何使用async实现这一点。请注意,浏览器已经弃用了
async:false
,现在在使用它时,会在控制台中警告弃用