Can't get specific element of API response [JS] - javascript

I can't seem to get a specific part out of the response below, using the code that is also below. I get an "undefined" in the console log when I try to use response["minecraft.net"]. Can someone explain to me why this isn't working?
Response:
"[{"minecraft.net":"green"},{"session.minecraft.net":"green"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"green"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"green"}]"
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mojang Servives Status</title>
</head>
<body>
<script>
var api = new XMLHttpRequest();
api.open("GET", "https://status.mojang.com/check", false);
api.send();
console.log(api);
var response = JSON.parse(api.response)
console.log(response["minecraft.net"]);
</script>
</body>
</html>

The response is wrapped inside of an array. So you'd use response[0]['minecraft.net']

Related

How to import pdf.js using cdn

I wanted to use pdf.js for a project of mine but I faced an issue of importing it, basically, the CDN doesn't work
Here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist#2.7.570/build/pdf.min.js"></script>
</head>
<body>
<canvas id="my-canvas"></canvas>
<script>
pdfjsLib.getDocument('./ahmed.pdf').then(doc => {
console.log("this file has" + doc._pdfInfo.numPages);
});
</script>
</body>
</html>
and that is the errors that my console shows
Deprecated API usage: No "GlobalWorkerOptions.workerSrc" specified.
Uncaught TypeError: pdfjsLib.getDocument(...).then is not a function
So what should i do to solve this problem and thank you so much
You have to set GlobalWorkerOptions.workerSrc to /build/pdf.worker(.min).js of same version:
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/pdfjs-dist#2.7.570/build/pdf.worker.min.js"; 👈
pdfjsLib.getDocument('./ahmed.pdf').promise.then(doc => {
console.log(`This document has ${doc._pdfInfo.numPages} pages.");
});
And, as #Pasi has mentioned, you have to promisify .getDocument() by chaining .promise on it. Without it, there is no .then().
See the "Hello World with document load error handling" example on this page to get started: https://mozilla.github.io/pdf.js/examples/
(Your snippet is missing .promise after getDocument() and setting the workerSrc property)

How to run javascript selenium function using a html button?

This code works fine but I need to run this script on a html button means when the button is clicked then this function get executed!!!
async function example() {
let driver = await new Builder().forBrowser("chrome").build();
await driver.get("https://google.com");
}
example();
I tried this but not working,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Javascript Testing</title>
</head>
<body>
<script type="text/Javascript">
async function example()
{
const {Builder, By, Key, util} = require("selenium-webdriver")
let driver = new Builder().forBrowser("chrome").build();
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("Selenium",KEY.RETURN);
}
</script>
<button onClick="example()">Click me</button>
</body>
</html>
Error:
index.html Uncaught ReferenceError: example is not defined
at HTMLButtonElement.onclick
and, I also tried it with external js but it still not working
selenium-webdriver cannot run inside a web browser, the APIs it needs are not available.
You would need to rewrite the JS so it runs in Node.js.
If you wanted to access it from a web browser, then you would need to write a web service front end for it.
You could then access that web service from the browser with, for example, fetch.

How to get content of body tag and store it in a variable by using getElementsByTagName() method

I'm trying to get value from body tag. First i have created ajax request in that when request get processed and returns the response to same page in that i i have taken a variable temp. in that variable whole html code is getting store with the output(now here output is content of body tag). like e.g in body tag some numbers are there. Now i want only the body content is get stored in the another variable by using getElementsByTagName method. i have tried with this but it shows getElementsByTagName is not a function. Please show me how to store the contents of body tag in a variable. This is related question to this question. Please do check for more details.
request.onreadystatechange= function()
{
if(this.readyState === 4 && this.status === 200)
{
var temp = this.responseText;
var tepo = temp.getElementsByTagName("body");
console.log(tepo);
}
};
This is result of this.responseText
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
-0.2
</body>
</html>
In your jsp page ,do like below :
//this will return as response to ajax
<% out.println("-0.2"); %>
And you will need to get that value :
var temp = this.responseText;//here temp will have -0.2
console.log(temp);
alert(temp);

could not load json data in d3 js

i am new in D3 JS. I would like to load json data from D3 js. Here, I am not sure if json is loaded. Here, non of message is appear. I want show html and json data ,
mydata.json
[
{"name":"Hira","age":35},
{"name":"Basu","age":26},
{"name":"Mukhia","age":30}
]
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
d3.json("./mydata.json", function(data) {
console.log(data);
});
</script>
</body>
</html>
Hope if you guys can give some hints.
here, I got a solution.
d3.json("./mydata.json").then(function(data){
console.log(data);
});
In new version we have to specify then function . In then function we have to make call back function.

How tho fetch the json data in Javascript for the image attached,it says undefined

image from html codethis is the screenshot of the consolestrong text
2 images attached
When i am trying to fetch the id its saying Undefined. i am trying like response[0].id . if we write like response[0] it shows json object also.
Also if we copy the json data to console and paste the value of response to a variable and then do response[0].id then it works. but from code it doesnot.
Seems a problem with your JSON data.
This is the code i have written working fine with javascript.
<!doctype html>
<html>
<head >
<title>
stack
</title>
<meta charset="utf-8">
</head>
<body>
<button onclick="myFunction()">Click me</button>
</body>
<script>
var response=[{"id":"1", "name":"abhishek","gender":"M"},{"id":"1", "name":"abhishek","gender":"M"}];
function myFunction() {
console.log(response[0].id);
}
</script>
</html>
Hello can you please try something like:
var listdata = [{"id":"1", "name":"abhishek","gender":"M"},{"id":"2", "name":"yogesh","gender":"M"},{"id":"3", "name":"rubi","gender":"F"}];
console.dir(listdata[0].id);
alert(listdata[0].id);
you can see running example here

Categories

Resources