gpt4 book ai didi

jquery - AJAX 全局变量

转载 作者:行者123 更新时间:2023-12-01 06:17:23 25 4
gpt4 key购买 nike

我正在尝试执行此操作,这会返回“未定义”:

$.ajax({
url : 'curriculum/read_types',
type : 'GET',
dataType : 'JSON',
success : function(data) {
return data;
}
})

但是如果我这样做:

$.ajax({
url : 'curriculum/read_types',
type : 'GET',
dataType : 'JSON',
success : function(data) {
console.log(data);
}
})

它在控制台上写入整个 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()
<小时/>

最终它成功了,但有一个 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;
}
}

最佳答案

回调函数(如成功处理程序)是注册的异步事件,在 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
}

更新:

read_types : function() {
$.getJSON('curriculum/read_types', function(data) {
return data;
});
}

上面的代码根本就是你做不到的。以下是流程的粗略描述:

  • read_types function is called from some other process.

  • The $.getJSON function is called with 2 arguments: path and callback handler.

  • The read_types function finishes processing and reaches the end.

  • Next, while the read_types method is in the process of finishing up, the getJSON function makes an HTTP GET request to your URL.
  • The data is received in the response and passed to the callback handler as an argument assigned to the parameter "data".
  • When you call return data; you are returning data to the anonymous success callback function, not read_types. Thus, the return statement essentially does nothing.

同步请求示例:

现在,您可以向服务器发出同步请求,但强烈建议不要这样做,因为它会对 View 产生影响。

但是,这是一个示例,仅用于学术目的。我绝不会提倡使用这种策略,除非你真的真的知道自己在做什么:

注意:除非您知道自己在做什么,否则不要在生产中使用它!

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;
}

关于jquery - AJAX 全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com