I am trying to use wikipedia api in order to get the content of a page by hitting a button. My code seems not to be working. In fact, when I hit my button, I get nothing in return.
What am i doing wrong? I saw some tutorials, but I can't get the code working.
Here's my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie-edge">
<title>Raccolta differenziata</title>
<script src="jquery-3.5.0.js"></script>
<script type="text/javascript" src="funzioniGoto.js"></script>
</head>
<body>
<p> aggiungere api wikipedia :)</p>
<div class="container">
<h1>Wikipedia Search</h1>
<input type="text" id="sTerm" name="search" placeholder="Search..."></input>
<button id='go' type="button">Submit</button>
<div id="data"></div>
</div>
<script>
$('#go').click(() => {
searchTerm = $('#sTerm').val()
$.ajax({
url: 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extract&titles='+searchTerm+'&redirects=true',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
method: 'GET',
dataType: 'jsonp',
data: '',
success: function(data) {
dataNum = Object.keys(data.query.pages)[0]
$('#data').empty()
$('#data').append('<h1>${data.query.pages[dataNum].title}</h1>') // virgoletta `
$('#data').append(data.query.pages[dataNum].extract)
}
})
})
</script>
</body>
</html>
Also, is there a way to find out what's happening? Refreshing the browser and using developer tools is not enough.
Basically, my idea is to open an html page and then find some wikipedia content (which my api should get from a page/article).
Errors:
appears "${data.query.pages[dataNum].title}"
A cookie associated with a cross-site resource at http://en.wikipedia.org/ was set without the
SameSite
attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set withSameSite=None
andSecure
. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.