Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
Javascript 在jquery中从ajax请求分配值_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 在jquery中从ajax请求分配值

Javascript 在jquery中从ajax请求分配值,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有以下jquery代码 var test = "test"; $.ajax({ url: root + "/servletPath", type: "GET", success: function (text) { alert(text); // returns the right value test = text; }, error: function () { } }); // prints "tes

我有以下jquery代码

var test = "test";

$.ajax({
    url: root + "/servletPath",
    type: "GET",
    success: function (text) {
        alert(text); // returns the right value
        test = text;

    },
    error: function () {

    }
});

// prints "test" and not the value that should be assigned in the success function
alert(test) 

在赋值之前,您正在警告变量
test
<代码>$。默认情况下,ajax是异步的

可能的解决方案:

var test = "test";

$.ajax({
    url: root + "/servletPath",
    type: "GET",
    success: function (text) {
        test = text;

        alertTest();
    },
    error: function () {

    }
});

function alertTest(){
    alert(test);
};

您还可以在
$.ajax
方法上将
async
属性设置为
false
,以同步运行代码

在赋值之前,您正在警告变量
test
<代码>$。默认情况下,ajax是异步的

可能的解决方案:

var test = "test";

$.ajax({
    url: root + "/servletPath",
    type: "GET",
    success: function (text) {
        test = text;

        alertTest();
    },
    error: function () {

    }
});

function alertTest(){
    alert(test);
};

您还可以在
$.ajax
方法上将
async
属性设置为
false
,以同步运行代码

在赋值之前,您正在警告变量
test
<代码>$。默认情况下,ajax是异步的

可能的解决方案:

var test = "test";

$.ajax({
    url: root + "/servletPath",
    type: "GET",
    success: function (text) {
        test = text;

        alertTest();
    },
    error: function () {

    }
});

function alertTest(){
    alert(test);
};

您还可以在
$.ajax
方法上将
async
属性设置为
false
,以同步运行代码

在赋值之前,您正在警告变量
test
<代码>$。默认情况下,ajax是异步的

可能的解决方案:

var test = "test";

$.ajax({
    url: root + "/servletPath",
    type: "GET",
    success: function (text) {
        test = text;

        alertTest();
    },
    error: function () {

    }
});

function alertTest(){
    alert(test);
};

您还可以在
$.ajax
方法上将
async
属性设置为
false
,以同步运行代码

AJAX是异步的。您应该像在异步运行AJAX时一样使用回调函数。AJAX是异步的。您应该像在异步运行AJAX时一样使用回调函数。AJAX是异步的。您应该像在异步运行AJAX时一样使用回调函数。AJAX是异步的。您应该像在异步运行AJAX时一样使用回调函数。