如何从ajax中写入console.log()调用两个级别的深度



请注意:我是使用ajax的新手,对不同页面之间的错误处理一无所知。


我正在从ajax调用中调用ajax调用;并且想要从两级深的第二页获得console.log()返回。链接到相关问题——但据我所知,这只是一个层次的深度,而不是两个层次。

我使用此代码得到的返回是第二个页面的HTML代码的一部分,而不是来自该writefile页面的console.log()调用。我怎样才能让它发挥作用?

function update_static_HTML(url,filename,cdn){
console.log('GET PAGE CONTEN FOR URL = '+url);
console.log('cdn = '+cdn);  
$.ajax({
url: url,
dataType: "html",
success: function(data) {
console.log('success - 1st');
var save_to_file_url = 'https://www.example.com/writefile?name='+filename+'&cdn='+cdn;
$.ajax({
type: 'POST',
url: save_to_file_url,
dataType: "html",
data: {
content: data 
},
success: function( result ) {
console.log('success - 2nd');

console.log( result ); // << returns page content, not console.log value
},
error:function(error){
console.log('ajax Error ' + JSON.stringify(error));
} 
});
},
error:function(error){
console.log('ajax Error ' + JSON.stringify(error));
} 
}); 
return false;
} 

更新:

我现在明白了,我需要以某种方式使用jqXHR通过一些回调来处理它。

我不清楚如何实现建议的代码:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.get( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});

它的哪一部分需要放到哪一页上?

作为临时解决方法,我现在正在将调试注释写入返回页面中的id,并从中获取调试信息。请注意,这不是我的问题的答案,而是一种仍然使用console.log来帮助我调试代码的方法。

...
success: function( result ) {
var info = $(result).find('#info').html();
console.log(info);
},
...

最新更新