Javascript - 无法从嵌套函数访问本地范围对象
我试图让一个函数从另一个页面上的 php 文件中获取一个对象。我正在使用 jQuery ajax 函数来执行 json 抓取,它工作正常。问题是当我尝试从函数返回该对象时。
我第一次记录该对象(在 success 函数内)时,它在控制台中是正确的,但函数 getGantt() 返回的对象记录为“未定义”。
如何从函数中取出这个对象?
我的代码:
function getGantt(requestNumber){
var ganttObject;
$.ajax({
type: "POST",
url: "get_gantt.php",
data: {request_number: requestNumber},
success: function(returnValue){
ganttObject = $.parseJSON(returnValue);
console.log(ganttObject); //this logs a correct object in the console
}
});
return ganttObject;
}
$(function(){ //document ready function
var requestNumber = $('#request_number').text();
var ganttObject = getGantt(requestNumber);
console.log(ganttObject); //this logs "undefined"
}); //end document ready function
I am trying to have a function grab an object from a php file on another page. I'm using the jQuery ajax function to to do the json grab, which is working correctly. The issue is when I try to return that object from the function.
The first time I log the object (from within the success function) it is correct in the console, but the returned object from the function getGantt() logs as "undefined".
How do I get this object out of the function?
My code:
function getGantt(requestNumber){
var ganttObject;
$.ajax({
type: "POST",
url: "get_gantt.php",
data: {request_number: requestNumber},
success: function(returnValue){
ganttObject = $.parseJSON(returnValue);
console.log(ganttObject); //this logs a correct object in the console
}
});
return ganttObject;
}
$(function(){ //document ready function
var requestNumber = $('#request_number').text();
var ganttObject = getGantt(requestNumber);
console.log(ganttObject); //this logs "undefined"
}); //end document ready function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ajax 中的 A 是缩写词的重要组成部分。异步 JavaScript 和 XML 是异步的。
$.ajax({success:someFunction})
表示发出 HTTP 请求,当响应到达时,运行someFunction
return ganttObject< /code> 在响应到达之前运行。
您应该对内部
someFunction
中的数据执行任何您想要执行的操作,而不是尝试将数据返回到调用函数。The A in Ajax is an important part of the acronym. Asynchronous JavaScript and XML is asynchronous.
$.ajax({success:someFunction})
means Make an HTTP request and when the response arrives, runsomeFunction
return ganttObject
runs before the response arrives.You should do anything you want to do with the data inside
someFunction
and not try to return data to the calling function.AJAX 中的 A 代表异步。因此,调用立即返回,并且一旦完成,就会调用成功回调。
因此,只需更改代码以使用回调即可:
顺便说一句,我还删除了这个 parseJSON 内容 - 将 dataType 设置为 json 即可完成工作,并且不那么脏。
The A in AJAX stands for asynchronous. So the call immediately returns and as soon as it finishes, the success callback is called.
So, simply change your code to use a callback:
Btw, I've also removed this parseJSON stuff - setting dataType to json does the job and is less dirty.
我至少知道为什么它不归还。 ganttObject 可能位于相同的范围内,但 success 函数最终在 XMLHTTP 对象的 readyState 回调中运行,因此它与 getGantt 函数位于不同的线程上。您可以将 $(function(){... 代码与您的成功函数分开吗?
I know why it's not returning it at least. The ganttObject may be in the same scope, but the success function is ultimately running in the readyState callback from the XMLHTTP object, so it's on a different thread than the getGantt function. Can you make the $(function(){... code apart of your success function?