0

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:

hellomynameisA
  • 546
  • 1
  • 7
  • 28
  • 1
    in the url you are using, the param `prop` has an incorrect value i.e. `extract`: missing `s` it should be : `prop=extracts` and you click handler should use `#go` to find the button to register handler correctly – developer May 18 '20 at 12:10

1 Answers1

1

Since you are doing a click on id , you need to use #go. Can you try to use -

$('#go').click(() 

Js Fiddle here

Mukesh Keshu
  • 467
  • 2
  • 13