<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Hi <font size="2"><span style="font-size:11pt;">everyone</span></font>,<br>
<br>
I'm developing an Ajax webservice that query Wikimedia Api Service, but I get a very strange behavior; in fact my query sometimes works perfectly and sometimes doesn't work at all (301 internal redirect) .<br>
<br>
This is the response by the server: <span class="treeIcon" role="presentation"></span><span class="treeLabel stringLabel" data-level="1"><span style="font-size: 9pt;">
<span style="color: rgb(23, 78, 134);">X-Cors-Redirect-1: </span></span><span><span class="objectBox objectBox-string"><span style="font-size: 9pt; color: rgb(23, 78, 134);">301
</span><a class="url" title="https://commons.wikimedia.org/w/api.php?origin=*&action=query&list=search&format=json&srsearch=File:manet+incategory:Artworks_with_Wikidata_item%7CArtworks_without_Wikidata_item%7Cmanet_in_art%7C+fileh:%3E600&%20&prop=imageinfo%7Cpageids%7Ctitles&srnamespace=6&rawcontinue=&srinfo=totalhits%7Csuggestion&srlimit=20&iilimit=10&iiprop=timestamp%7Cuser%7Curl%7Csize%7Csha1%7Cmime%7Cmetadata&sroffset=0&callback=?" draggable="false"><span style="font-size: 9pt; color: rgb(23, 78, 134);">https://commons.wikimedia.…metadata&sroffset=0&callback=?</span></a></span></span></span><span>
<div class="treeValueCellDivider"><span class="objectBox objectBox-string"><br>
Could you help me to understand why or what I miss in my code?<br>
</span></div>
</span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><span class="treeLabel stringLabel" data-level="1"></span><br>
This is my code : <br>
<br>
<span style="font-size: 9pt;"></span><span style="font-size: 9pt; color: rgb(23, 78, 134);">$.ajaxPrefilter(function (options) {</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> if (options.crossDomain && jQuery.support.cors) {</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> const https = (window.location.protocol === 'http:' ? 'http:' : 'https:');</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> options.url = https + '//cors-anywhere.herokuapp.com/' + options.url;</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> }</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> if ( !options.beforeSend) {</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> options.beforeSend = function (xhr) {
</span><br>
<span style="font-size: 10pt;"><span style="font-size: 9pt; color: rgb(23, 78, 134);"> xhr.setRequestHeader('Api-User-Agent', 'OpenArtImages/Beta (</span><span style="font-size: 9pt; color: rgb(23, 78, 134);">http://localhost:8080</span><span style="font-size: 9pt; color: rgb(23, 78, 134);">;
viviana.paga@hotmail.it; Wikipedia User: Vivsss)');</span></span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> xhr.setRequestHeader('Origin', 'http://localhost:8080');</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> xhr.setRequestHeader('Strict-Transport-Security', 'max-age=106384710; includeSubDomains; preload');</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> </span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> xhr.withCredentials = true;</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> }</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> }</span><br>
<span style="font-size: 10pt;"><span style="font-size: 9pt; color: rgb(23, 78, 134);"> });</span><br>
<br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);">firstRequest = $.get( 'https://commons.wikipedia.org/w/api.php?origin=*&action=query&list=search&format=json&srsearch=File:'+inputWord+incategory+'+fileh:>600& &prop=imageinfo|pageids|titles&srnamespace=6&rawcontinue=&srinfo=totalhits|suggestion&srlimit='+limit+'&iiprop=timestamp|user|url|size|sha1|mime|metadata'+offset+'&callback=?',</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> function (response) {</span><span style="font-size: 9pt; color: rgb(23, 78, 134);">
</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);"> /////</span><span style="font-size: 9pt; color: rgb(23, 78, 134);"> ........ I get Data ......... /////</span><br>
<span style="font-size: 9pt; color: rgb(23, 78, 134);">}<br>
<br>
<br>
<span style="font-size: 10pt; color: rgb(0, 0, 0);">Thank you so much,</span><br>
<span style="font-size: 10pt; color: rgb(0, 0, 0);">Viviana Pa</span><span style="font-size: 10pt; color: rgb(0, 0, 0);">g</span><span style="font-size: 10pt; color: rgb(0, 0, 0);">a</span><br>
</span></span><span style="font-size: 9pt;"></span></div>
</body>
</html>