如何在两层嵌套Ajax调用中获取console.log输出内容?
嵌套Ajax调用获取第二层页面console.log输出的问题
我是Ajax新手,不了解跨页面错误处理。现在做嵌套Ajax调用(在一个Ajax调用内发起另一个Ajax调用),想要获取第二层writefile页面的console.log()返回内容。之前看到的方案只适用于单层Ajax调用,不适用于两层嵌套的情况。目前代码返回的是第二个页面(writefile页面)的部分HTML代码,而非该页面内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的回调来处理,但不清楚示例代码的各个部分应该放在哪个页面?
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" ); });
内容的提问来源于stack exchange,提问作者MeSo2
相关产品推荐
相关产品推荐

