Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 从XMLHttpRequest函数返回JSON_Javascript_Json_Xmlhttprequest_Return - Fatal编程技术网

Javascript 从XMLHttpRequest函数返回JSON

Javascript 从XMLHttpRequest函数返回JSON,javascript,json,xmlhttprequest,return,Javascript,Json,Xmlhttprequest,Return,您好,我一直在尝试从XMLHttpRequest函数返回数据。我尝试了许多不同的方法,但当我尝试从函数外部将数据输出到控制台时,我得到的唯一结果是“未定义”。只有从函数本身内部执行时,它才有效 <script> var object; function loadJSON(path, success, error) { var xhr = new XMLHttpRequest(); var obj1; xhr.onreadystatechange = func

您好,我一直在尝试从XMLHttpRequest函数返回数据。我尝试了许多不同的方法,但当我尝试从函数外部将数据输出到控制台时,我得到的唯一结果是“未定义”。只有从函数本身内部执行时,它才有效

<script>
var object;

function loadJSON(path, success, error) {
    var xhr = new XMLHttpRequest();
    var obj1;
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                if (success)
                success(JSON.parse(xhr.responseText));
                //console.log(data); works here!
            } else {
                if (error)
                error(xhr);
            }
        }
    };
    xhr.open("GET", path, true);
    xhr.send();
}


object = loadJSON('jconfig.json',
function (data) { console.log(data); return($data);/*works here! but does not return!*/ },
function (xhr) { console.error(xhr); }
);

console.log(object);//does not work here
</script>

var对象;
函数loadJSON(路径、成功、错误){
var xhr=new XMLHttpRequest();
var-obj1;
xhr.onreadystatechange=函数(){
if(xhr.readyState==XMLHttpRequest.DONE){
如果(xhr.status==200){
如果(成功)
成功(JSON.parse(xhr.responseText));
//log(数据);在这里工作!
}否则{
如果(错误)
误差(xhr);
}
}
};
xhr.open(“GET”,path,true);
xhr.send();
}
object=loadJSON('jconfig.json',
函数(数据){console.log(数据);return($data);/*在这里工作!但不返回!*/},
函数(xhr){console.error(xhr);}
);
console.log(对象)//这里不行
我知道这是一个非常简单的问题,但我已经被这个问题困扰了一个多小时了,其他类似问题的答案似乎无法帮助我克服这个障碍。非常感谢您的帮助

编辑:我用一些建议更新了代码,但我仍然无法让ti工作。任何让上面的代码最终返回我可以在函数之外使用的东西的建议。

在调用laodJSON()函数之后执行line console.log(object),直到那时才加载JSON对象

这与回调和异步函数有关。loadJSON()只能在从服务器获得响应时实际加载JSON

相反,如果要在loadJSON()函数之外调用JSON对象,则需要使用回调函数。大概是这样的:

<script>
var object;

function loadJSON(path, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                // Here the callback gets implemented
                object = JSON.parse(xhr.responseText);
                callback();
            } else {

            }
        }
    };

    xhr.open("GET", path, true);
    xhr.send();
    return xhr.onreadystatechange();
}

loadJSON('jconfig.json', function printJSONObject(){
          console.log(object);
    });

// this will not work unless you get the response
console.log(object);

</script>

var对象;
函数loadJSON(路径,回调){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==XMLHttpRequest.DONE){
如果(xhr.status==200){
//这里实现了回调
object=JSON.parse(xhr.responseText);
回调();
}否则{
}
}
};
xhr.open(“GET”,path,true);
xhr.send();
返回xhr.onreadystatechange();
}
loadJSON('jconfig.json',函数printJSONObject(){
console.log(对象);
});
//除非你得到答复,否则这是行不通的
console.log(对象);
更新:通过使用回调从异步函数中“返回”一个值是没有意义的,因为下一行代码将在不等待响应的情况下立即执行

相反,如果您想在发送XHR请求的函数之外使用对象,请在回调函数内部实现所有内容。

在调用laodJSON()函数之后立即执行line console.log(对象),在此之前不会加载JSON对象

这与回调和异步函数有关。loadJSON()只能在从服务器获得响应时实际加载JSON

相反,如果要在loadJSON()函数之外调用JSON对象,则需要使用回调函数。大概是这样的:

<script>
var object;

function loadJSON(path, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                // Here the callback gets implemented
                object = JSON.parse(xhr.responseText);
                callback();
            } else {

            }
        }
    };

    xhr.open("GET", path, true);
    xhr.send();
    return xhr.onreadystatechange();
}

loadJSON('jconfig.json', function printJSONObject(){
          console.log(object);
    });

// this will not work unless you get the response
console.log(object);

</script>

var对象;
函数loadJSON(路径,回调){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==XMLHttpRequest.DONE){
如果(xhr.status==200){
//这里实现了回调
object=JSON.parse(xhr.responseText);
回调();
}否则{
}
}
};
xhr.open(“GET”,path,true);
xhr.send();
返回xhr.onreadystatechange();
}
loadJSON('jconfig.json',函数printJSONObject(){
console.log(对象);
});
//除非你得到答复,否则这是行不通的
console.log(对象);
更新:通过使用回调从异步函数中“返回”一个值是没有意义的,因为下一行代码将在不等待响应的情况下立即执行


相反,如果您想在发送XHR请求的函数外部使用对象,请在回调函数内部实现所有内容。

XHR是异步的。正如我上面的先生所说。您可以选择将成功/错误回调传递给
loadJSON
,或将本机XHR包装在本机/库承诺中。我尝试添加回调,但似乎仍然无法确定如何返回值。XHR是异步的。我上面的先生说了什么。您可以选择将成功/错误回调传递给
loadJSON
,或将本机XHR包装在本机/库承诺中。我尝试添加回调,但似乎仍不知道如何返回值。感谢您清除此问题。我想我只是希望用php来同步执行。。。这意味着当JSON对象可用时,您将通过PHP生成JavaScript代码?谢谢您的澄清。我想我只是希望用php来同步执行。。。这意味着当JSON对象可用时,您将通过PHP生成JavaScript代码?