Mar*_*rio 1 jquery global-variables
我正在尝试这样做,返回'undefined':
$.ajax({
url : 'curriculum/read_types',
type : 'GET',
dataType : 'JSON',
success : function(data) {
return data;
}
})
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
$.ajax({
url : 'curriculum/read_types',
type : 'GET',
dataType : 'JSON',
success : function(data) {
console.log(data);
}
})
Run Code Online (Sandbox Code Playgroud)
它在控制台上写了一个完整的JSON对象,所以我知道存在数据.
我怎么能归还这些数据?
我想做的是下一个:
var curriculum = {
add : function() {
html = [];
html.push('<select name="type" required>');
html.push('<option value="0">Grupo general...</option>');
var types = curriculum.read_types();
$.each(types, function(k,v) {
html.push('<option value="'+v+'">'+v+'</option>')
})
html.push('</select>');
content.show('Añadir imagen a curriculum',html.join(''));
},
read_types : function() {
$.getJSON('curriculum/read_types', function(data) {
return data;
})
}
}
curriculun.add()
Run Code Online (Sandbox Code Playgroud)
最后它管理但使用asyn:false请求:
var curriculum = {
add : function() {
html = [];
html.push('<select name="type" required>');
html.push('<option value="0">Grupo general...</option>');
var types = curriculum.read_types();
$.each(types, function(k,v) {
html.push('<option value="'+v+'">'+v+'</option>')
})
html.push('</select>')
content.show('Añadir imagen a curriculum',html.join(''));
},
read_types : function() {
var a;
$.ajax({
url : 'curriculum/read_types',
type : 'GET',
async : false,
contentType : 'JSON',
success : function(data) {
a = data;
}
})
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
回调函数(如成功处理程序)是已注册的异步事件,一旦AJAX请求完成就会触发,并将成功结果返回给客户端浏览器.由于事件已注册,因此它不会阻止您运行AJAX请求的功能.
为了处理数据,只需将数据移交给另一个函数,如下所示:
$.ajax({
url : 'curriculum/read_types',
type : 'GET',
dataType : 'JSON',
success : function(data) {
console.log(data):
// process the results
processData(data);
}
});
function processData(data) {
// do stuff with the data here
}
Run Code Online (Sandbox Code Playgroud)
更新:
read_types : function() {
$.getJSON('curriculum/read_types', function(data) {
return data;
});
}
Run Code Online (Sandbox Code Playgroud)
上面的代码只是你不能做的事情.以下是对流程的简要描述:
read_types从其他一些进程调用函数.使用2个参数调用$ .getJSON函数:path和callback handler.
read_types函数完成处理并到达结尾.
- 接下来,当read_types方法正在完成时,getJSON函数向您的URL发出HTTP GET请求.
- 数据在响应中接收,并作为分配给参数"data"的参数传递给回调处理程序.
- 当您调用时,
return data;您将数据返回到匿名成功回调函数,而不是read_types.因此,return语句基本上什么都不做.
现在,说到这一点,您可以向服务器发出同步请求,但由于它对视图的影响,强烈建议不要这样做.
但是,这是一个例子,仅用于学术目的.我永远不会主张使用这个策略,除非你真的真的知道你在做什么:
注意:除非您知道自己在做什么,否则不得在生产中使用!
function getData() {
var myDataObj = null;
// this will BLOCK execution of all other scripts on the page until
// the data is received!
$.ajax({
url : 'curriculum/read_types',
type : 'GET',
dataType : 'JSON',
success : function(data) {
console.log(data):
// we can assign this data to myDataObj in synchronous requests
myDataObj = data;
},
async: false /** Make a synchronous request **/
});
// Since we BLOCKED, this object is not null. If we used async: true
// (the default) then this would return null.
return myDataObj;
}
Run Code Online (Sandbox Code Playgroud)