我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何返回AJAX响应文本?
我正在调用一个javascript方法,它反过来发送Ajax请求并给出响应.我在回调方法中获得结果,如"成功".
在这里,Ajax返回一些结果,同时javascript方法返回结果(某些内容为undefined).
但它应该只返回ajax结果.
我发现的问题是,Javascript和Ajax都在同时执行.
如何阻止它们并首先执行Ajax,它的结果必须发送到返回结果的函数.
任何想法都是高度赞赏.. :)
$(document).ready(function(){
// Global function (will be include in any HTML file)
function m3_result(size_1, size_2, size_3){
$.get('http://www.google.com', function(data){
return data;
});
}
// Another function
function calculate(){
var size_1 = parseFloat($('#add_document_form #size_1').val());
var size_2 = parseFloat($('#add_document_form #size_2').val());
var size_3 = parseFloat($('#add_document_form #size_3').val());
var ax = m3_result(size_1, size_2, size_3);
alert(ax); // Here ax return: UNDEFINED
}
// Run
calculate();
});
Run Code Online (Sandbox Code Playgroud)
结果是“未定义的”,但是我希望calculate()将等待m3_result()执行。我看到当我添加$ .get()时这里出现了问题,但是它是必需的...
我正在搜索类似callback()的函数,但都不适合我的需要,或者我只是没有正确地说明这一点。任何帮助将不胜感激,谢谢。
GET URL将是本地语言,元素ID也可以。
我从API获取vimeo缩略图,我正在使用jQuery函数将数据附加到dom.
我正在尝试访问ajax之外的thumb_url,所以我可以将它返回给jQuery,但它不起作用.
function getThumb(vimeoVideoID) {
var thumb_url;
$.ajax({
type: 'GET',
url: 'http://vimeo.com/api/v2/video/' + vimeoVideoID + '.json',
jsonp: 'callback',
dataType: 'jsonp',
success: function (data) {
console.log(data[0].thumbnail_large);
thumb_url = data[0].thumbnail_large;
}
});
return thumb_url;
}
$('.video').each(function () {
var thumb_url = getThumb(this.id);
$(this).append('<img src="' + thumb_url + '" class="video_preview"/>');
});
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/gyQS4/2/ 求助?
可能重复:
如何返回AJAX响应文本?
我正在尝试使用以下代码.我知道在函数中的变量范围内我做的不对,但我无法弄清楚是什么(我主要是设计师,我很高兴我终于明白了JSON的含义).任何人都可以把我推向正确的方向吗?谢谢 :)
var list = new Array();
//Twitter
function twitter(photos){
$.each(photos.results, function(index, photo){
if (photo.entities.media){
list.push(photo.entities.media[0].media_url);
console.log(list); // working here, returns array
}
});
}
var url = "https://search.twitter.com/search.json?callback=?&q=%23pikachu&include_entities=true&count=50";
$.getJSON(url, twitter);
console.log(list); //not working here, returns []
Run Code Online (Sandbox Code Playgroud) 我试图从ajax成功函数返回值。但它什么也没返回。
JS
function calculate_total_percentage(course_log_id){
var total_percentage = 0;
$.ajax({
url:"teacher_internal_exam_management/get_exams_of_course_log/"+course_log_id,
type: "POST",
dataType: "json",
success: function (exams_of_course_log) {
for (var x = 0; x < exams_of_course_log.length; x++) {
total_percentage += parseInt(exams_of_course_log[x].marks_percentage);
}
alert(total_percentage);
return total_percentage;
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果我那样打
alert(calculate_total_percentage(course_log_id));
Run Code Online (Sandbox Code Playgroud)
然后显示“ 61”(由于调用alert(total_percentage);),然后显示“ undefined”为什么?它应该两次显示“ 61”?问题是什么?
所以我有一个javascript函数,我正在进行AJAX调用,以查看用户是在线还是离线.它看起来像这样.
function onlineStatus(){
$.ajax({
url: "assets/ajax/online-offline.php",
cache: false,
success: function(html){
return html;
}
});
}
Run Code Online (Sandbox Code Playgroud)
我想将此函数的值赋值为一个我可以使用的变量.
像这样的东西.
var test = onlineStatus();
if (test == "true")
alert("online");
else
alert("offline");
Run Code Online (Sandbox Code Playgroud)
这可能吗?我一定做错了,但无法弄清楚如何实现这个结果.谢谢
//编辑:感谢大家的帮助,抱歉,没有意识到这可能是一个重复的问题.我不知道最初要搜索什么,所以我没有看到任何相关内容.
为什么在第9行中定义的x未定义为第11行?
<script>
var x;
$.ajax({
dataType: "json",
url: myurl,
success: function(data){
console.log(data);
x = data;
document.write(x);
}
});
document.write(x);
</script>
Run Code Online (Sandbox Code Playgroud) 我想编写一个在客户端使用JavaScript运行的函数,但检查服务器端是否存在文件.我尝试使用Ajax.
function ROIFileExists(){
var fileAlreadyExists=undefined;
jQuery.ajax({
type: "POST",
url: "ROIFileExists.php",
data: { FileName: fileName},
cache: false
}).done(function( result ) {
fileAlreadyExists = (result!==0);
console.log('fileAlreadyExists='+fileAlreadyExists);
});
return fileAlreadyExists;
}
Run Code Online (Sandbox Code Playgroud)
问题是,由于Ajax是异步的,fileAlreadyExists因此通常undefined在Ajax块设置之前返回(as ).
javascript ×9
ajax ×8
jquery ×6
json ×2
asynchronous ×1
concurrency ×1
function ×1
php ×1
scope ×1
vimeo ×1