@@ -522,41 +522,63 @@ var handlePJAX = function() {
522522}
523523
524524
525- function fetchAndProcessData ( userInputUrl , repo , branch ) {
525+ function fetchAndProcessData ( changeIdBasedtUrl , changeNumberBasedUrl , repo , branch , number ) {
526526 $ . ajax ( {
527- url : userInputUrl ,
527+ url : changeIdBasedtUrl ,
528528 type : 'GET' ,
529- dataType : 'text' , // 期望服务器返回文本格式
530- crossDomain : true , // 明确告诉 jQuery 发送跨域请求
529+ dataType : 'text' ,
530+ crossDomain : true ,
531531 success : function ( data ) {
532532 try {
533- // 假设服务器返回的数据格式是 ")]}', followed by the actual JSON data"
534- // 我们需要移除前缀 ")]}'," 来获取正确的 JSON 字符串
535- var startIndex = data . indexOf ( '{' ) ;
536- var cleanedData = data . substring ( startIndex ) ;
537- var jsonData = JSON . parse ( cleanedData ) ;
538- console . log ( 'Received data:' , jsonData ) ;
539- createBtn ( ) ;
540- createGerritTreeContainer ( repo , branch ) ;
541- createGerritTree ( jsonData ) ;
542- clickNode ( ) ;
543-
544- // handleToggleBtn();
545- // hotkey();
546- // hackStyle();
547- console . log ( 'Tree built and inserted into DOM.' ) ;
533+ var startIndex = data . indexOf ( '{' ) ;
534+ var cleanedData = data . substring ( startIndex ) ;
535+ var jsonData = JSON . parse ( cleanedData ) ;
536+ console . log ( 'Received data:' , jsonData ) ;
537+ createBtn ( ) ;
538+ createGerritTreeContainer ( repo , branch ) ;
539+ createGerritTree ( jsonData ) ;
540+ clickNode ( ) ;
541+ console . log ( 'Tree built and inserted into DOM.' ) ;
548542 } catch ( e ) {
549- console . error ( "Error parsing JSON:" , e ) ;
550- // 在请求失败时,给出用户反馈
551- treeContainer . append ( '<p>Error parsing data. Please check the URL and try again.</p>' ) ;
543+ console . error ( "Error parsing JSON:" , e ) ;
544+ treeContainer . append ( '<p>Error parsing data. Please check the URL and try again.</p>' ) ;
552545 }
553546 } ,
554547 error : function ( xhr , status , error ) {
555- console . error ( "Error fetching data:" , error ) ;
556- // 在请求失败时,给出用户反馈
557- treeContainer . append ( '<p>Error fetching data. Please check the URL and try again.</p>' ) ;
548+ if ( xhr . status === 404 ) {
549+ // 如果返回404错误,使用changeNumberBasedUrl重新发起请求
550+ $ . ajax ( {
551+ url : changeNumberBasedUrl ,
552+ type : 'GET' ,
553+ dataType : 'text' ,
554+ crossDomain : true ,
555+ success : function ( data ) {
556+ try {
557+ var startIndex = data . indexOf ( '{' ) ;
558+ var cleanedData = data . substring ( startIndex ) ;
559+ var jsonData = JSON . parse ( cleanedData ) ;
560+ console . log ( 'Received data:' , jsonData ) ;
561+ createBtn ( ) ;
562+ createGerritTreeContainer ( repo , branch ) ;
563+ createGerritTree ( jsonData ) ;
564+ clickNode ( ) ;
565+ console . log ( 'Tree built and inserted into DOM.' ) ;
566+ } catch ( e ) {
567+ console . error ( "Error parsing JSON:" , e ) ;
568+ treeContainer . append ( '<p>Error parsing data. Please check the URL and try again.</p>' ) ;
569+ }
570+ } ,
571+ error : function ( xhr , status , error ) {
572+ console . error ( "Error fetching data:" , error ) ;
573+ treeContainer . append ( '<p>Error fetching data. Please check the URL and try again.</p>' ) ;
574+ }
575+ } ) ;
576+ } else {
577+ console . error ( "Error fetching data:" , error ) ;
578+ treeContainer . append ( '<p>Error fetching data. Please check the URL and try again.</p>' ) ;
579+ }
558580 }
559- } ) ;
581+ } ) ;
560582}
561583
562584
@@ -599,7 +621,8 @@ function refreshTree() {
599621
600622 // 拼接 URL
601623 var pathUrl = base + "changes/" + changeId + "/revisions/current/files" ;
602- fetchAndProcessData ( pathUrl , jsonData . project , jsonData . branch ) ;
624+ var pathUr2 = base + "changes/" + number + "/revisions/current/files" ;
625+ fetchAndProcessData ( pathUrl , pathUr2 , jsonData . project , jsonData . branch ) ;
603626 console . log ( "New URL:" , pathUrl ) ;
604627
605628 } catch ( e ) {
0 commit comments