Wikipedia api native javascript

Wikipedia api native javascript
0

#1

Hello . I can not get normal json data. The information what I receive is not complete.

My code

document.getElementById('search').addEventListener('click',function(val){
    let userInput = document.getElementById('userInput');
    let term = userInput.value;
    let wikiRequest = new XMLHttpRequest();
    
   
    let url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=god&origin=*&format=json';
    
    wikiRequest.open('GET', url)
    wikiRequest.onload = function() {
        let data = JSON.parse(wikiRequest.responseText);
     
        let array = data.query.search;
        for(var i= 0; i < array.length; i++){
            let output = document.getElementById('output');
            output.insertAdjacentHTML('beforeEnd', '<h3>'+array[i].title+'</h3>' + '<p>'+ array[i].snippet +'</p>' );
            // console.log(array[i]);
        }
        console.log(data);
    }
    wikiRequest.send();
} )

i think problem is in parameters of wiki api what i pointed out. I will be very grateful for any help.


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

When I run your code, I get a JSON data. What format are you wanting to get exactly?


#4

Hi, I want to receive full information about term what i searching for .


#5

The task is completed. Sorry my mistake. How can I close this topic?


#6

USE THIS PLEASE:

function Wiki(lang){
	this.lang = lang || "fr";
	this.inuse = false;
}
Wiki.prototype.research = function(s, callback){
	if(this.inuse){
		console.error("Wiki est déjà en cours d'utilisation !");
	} else {
		this.inuse = true;
		let r = new XMLHttpRequest();
		r.onload = function(){
			Wiki.prototype.inuse = false;
			let j = JSON.parse(r.responseText);
			callback(j);
		}
		r.open('GET', "https://" + this.lang +".wikipedia.org/w/api.php?%20action=opensearch&format=json&origin=*&profile=normal&search="+encodeURIComponent(s));
		r.send();
	}
}

//EXEMPLE
var c = new Wiki();
c.research("Victor Hugo", function(result){
	console.log(result);
});

#7