2017-12-01 2 views
0

필요한 일부 데이터는 위키피디아의 es (스페인어 버전)에서만 사용할 수 있으며 이것은 내 스크립트를 위반하고 있습니다.이 유용한 정보를 사용하여 가능한 한 데이터를 호출하고 있습니다. 볼이 https://en.wikipedia.org/w/api.php에서 호출하지만 난 여전히뿐만 아니라 영어 데이터가 필요하지만이 특정 데이터가 https://es.wikipedia.org/w/api.php위키 피 디아에서 여러 URL (언어)의 데이터를 제공하는 방법 API

에서만 사용할 수 있습니다

그래서 내가 두 API에서 인출이 변환 할 수있는 방법 JS의 내 지식이 너무 최소한의 URL 회신 할 때 감사드립니다.

$.ajax({ 
    url: 'https://en.wikipedia.org/w/api.php', 

    data: { 
     format: 'json', 
     action: 'parse', 
     page: this_target, 
     prop:'text', 
     section:0, 
    }, 
    dataType: 'jsonp', 
    success: function (data) { 
    //console.log(data) 
    $(id_target).find('.o-modal__inner').html(''); 
    $(id_target).find('.o-modal__title').html('').hide();  
    $(id_target).find('.o-modal').addClass('isOpen'); 
    if(!$(id_target).find('.a-more').hasClass('isActive')){ 
     $(id_target).find('.a-more').trigger('click'); 
    } 
    var markup = data.parse.text['*']; 
    var i = $('<div></div>').html(markup); 
    i.find('a').each(function() { $(this).replaceWith($(this).html()); }); 
    i.find('sup').remove(); 
    i.find('.mw-ext-cite-error').remove(); 
    setTimeout(function() { 
     $(id_target).find('.o-modal__title').html(data.parse.title).fadeIn(300); 
     $(id_target).find('.o-modal__inner').html($(i).find('p')); 
     $(id_target).find('.a-loader').hide(); 
     }, 1000); 
    } 
}); 

});

답변

0

당신은 success 콜백의 상단에 data.error를 확인하고 영어 페이지 그렇다면, 단지 하위 도메인 대체하여 아약스 요청을 다시 수 :

if (data.error && new URL(this.url).hostname === "en.wikipedia.org") { 
    this.url = this.url.replace('en', 'es') 
    $.ajax(this); 
    return; 
    } else if(data.error) { 
    console.log('No data to display from both english & spanish wiki'); 
    return; 
    } 
+0

나에게 프랑스어 말하기를 : 나는이 사이에 코드를 삽입입니다 라인 죄송합니다 JS에 대해 거의 알지 못해요. 그냥이 재미있는 정보를 추적했습니다 .... dataType : 'jsonp', if (data.error && new URL (this.url) .hostname === "en.wikipedia. org ") { this.url = this.url.replace ('en', 'es') $ .ajax (this); 반환; } success : function (data) { – user3677211

+0

아니요, 성공 콜백의 내부에 설정해야합니다. 성공 : function (data) {if (data.error && new URL (this.url) .hostname === "en.wikipedia.org") {...} ...},'. BTW, 나는 위키를 표시하는 데 아무 것도없는 경우를 처리하기 위해 답변을 업데이트했습니다. –

+0

감사합니다. 완벽하게 작동했습니다. – user3677211

관련 문제