admin管理员组文章数量:1024637
I have two functions. The first sets a variable then the second get's the information needed for the first function. The issue I was first having was that everything was getting returned before the ajax response was happening. Basically what I need to do is get a JSON object and set it to a variable, then uses certain aspects of that object and append them to items. here's what I have...
$(document).ready(function(){
$('#youtube-url').blur(function() {
$info = youtubeInfo($('#youtube-url').val());
console.log($info);
if($info.html){
//alert($info.thumbnail_url);
//$('#preview_video_thumb').attr('src', $info.thumbnail_url);
}
});
});
function youtubeInfo(url){
var odata = '';
var result = jQuery.ajax({
url: '<? echo base_url("videos/getOembed") ?>',
type: 'POST',
dataType: 'json',
async: false,
data: {url: url},
plete: function(xhr, textStatus) {
},
success: function(data, textStatus, xhr) {
return data;
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
}).responseText;
return result;
};
If I return the 'data' object from within the success callback it returns blank. if I do it how it is, the responseText of the AJAX call is a string and not JSON. any ideas? I basically need to set the $info variable in the first function to the AJAX response (JSON) of the second function.
I have two functions. The first sets a variable then the second get's the information needed for the first function. The issue I was first having was that everything was getting returned before the ajax response was happening. Basically what I need to do is get a JSON object and set it to a variable, then uses certain aspects of that object and append them to items. here's what I have...
$(document).ready(function(){
$('#youtube-url').blur(function() {
$info = youtubeInfo($('#youtube-url').val());
console.log($info);
if($info.html){
//alert($info.thumbnail_url);
//$('#preview_video_thumb').attr('src', $info.thumbnail_url);
}
});
});
function youtubeInfo(url){
var odata = '';
var result = jQuery.ajax({
url: '<? echo base_url("videos/getOembed") ?>',
type: 'POST',
dataType: 'json',
async: false,
data: {url: url},
plete: function(xhr, textStatus) {
},
success: function(data, textStatus, xhr) {
return data;
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
}).responseText;
return result;
};
If I return the 'data' object from within the success callback it returns blank. if I do it how it is, the responseText of the AJAX call is a string and not JSON. any ideas? I basically need to set the $info variable in the first function to the AJAX response (JSON) of the second function.
Share Improve this question asked May 29, 2012 at 19:54 G.ThompsonG.Thompson 8272 gold badges11 silver badges25 bronze badges2 Answers
Reset to default 3Remove .responseText
and use this:
$('#youtube-url').blur(function() {
youtubeInfo($('#youtube-url').val()).done(function($info){
if($info.html){
//alert($info.thumbnail_url);
//$('#preview_video_thumb').attr('src', $info.thumbnail_url);
}
});
});
The return data
statement you have in the success function is not doing what you intend. It is not actually returning out of the ajax call, but rather returning out of the success function. What you probably want is something like this instead:
function youtubeInfo(url){
var odata = '';
var result = jQuery.ajax({
url: '<? echo base_url("videos/getOembed") ?>',
type: 'POST',
dataType: 'json',
async: false,
data: {url: url},
plete: function(xhr, textStatus) {
},
success: function(data, textStatus, xhr) {
odata = data;
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
return odata;
};
I have two functions. The first sets a variable then the second get's the information needed for the first function. The issue I was first having was that everything was getting returned before the ajax response was happening. Basically what I need to do is get a JSON object and set it to a variable, then uses certain aspects of that object and append them to items. here's what I have...
$(document).ready(function(){
$('#youtube-url').blur(function() {
$info = youtubeInfo($('#youtube-url').val());
console.log($info);
if($info.html){
//alert($info.thumbnail_url);
//$('#preview_video_thumb').attr('src', $info.thumbnail_url);
}
});
});
function youtubeInfo(url){
var odata = '';
var result = jQuery.ajax({
url: '<? echo base_url("videos/getOembed") ?>',
type: 'POST',
dataType: 'json',
async: false,
data: {url: url},
plete: function(xhr, textStatus) {
},
success: function(data, textStatus, xhr) {
return data;
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
}).responseText;
return result;
};
If I return the 'data' object from within the success callback it returns blank. if I do it how it is, the responseText of the AJAX call is a string and not JSON. any ideas? I basically need to set the $info variable in the first function to the AJAX response (JSON) of the second function.
I have two functions. The first sets a variable then the second get's the information needed for the first function. The issue I was first having was that everything was getting returned before the ajax response was happening. Basically what I need to do is get a JSON object and set it to a variable, then uses certain aspects of that object and append them to items. here's what I have...
$(document).ready(function(){
$('#youtube-url').blur(function() {
$info = youtubeInfo($('#youtube-url').val());
console.log($info);
if($info.html){
//alert($info.thumbnail_url);
//$('#preview_video_thumb').attr('src', $info.thumbnail_url);
}
});
});
function youtubeInfo(url){
var odata = '';
var result = jQuery.ajax({
url: '<? echo base_url("videos/getOembed") ?>',
type: 'POST',
dataType: 'json',
async: false,
data: {url: url},
plete: function(xhr, textStatus) {
},
success: function(data, textStatus, xhr) {
return data;
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
}).responseText;
return result;
};
If I return the 'data' object from within the success callback it returns blank. if I do it how it is, the responseText of the AJAX call is a string and not JSON. any ideas? I basically need to set the $info variable in the first function to the AJAX response (JSON) of the second function.
Share Improve this question asked May 29, 2012 at 19:54 G.ThompsonG.Thompson 8272 gold badges11 silver badges25 bronze badges2 Answers
Reset to default 3Remove .responseText
and use this:
$('#youtube-url').blur(function() {
youtubeInfo($('#youtube-url').val()).done(function($info){
if($info.html){
//alert($info.thumbnail_url);
//$('#preview_video_thumb').attr('src', $info.thumbnail_url);
}
});
});
The return data
statement you have in the success function is not doing what you intend. It is not actually returning out of the ajax call, but rather returning out of the success function. What you probably want is something like this instead:
function youtubeInfo(url){
var odata = '';
var result = jQuery.ajax({
url: '<? echo base_url("videos/getOembed") ?>',
type: 'POST',
dataType: 'json',
async: false,
data: {url: url},
plete: function(xhr, textStatus) {
},
success: function(data, textStatus, xhr) {
odata = data;
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
return odata;
};
本文标签: javascriptJquery AJAX response JSON in variableStack Overflow
版权声明:本文标题:javascript - Jquery AJAX response JSON in variable. - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745620926a2159566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论