17, Jan 2023
How to get file data from url in javascript ?

To fetch data from a URL in JavaScript, you can use the fetch()
function. The fetch()
function is a Promise-based API that allows you to make HTTP requests to a server.
Here’s an example of how to use fetch()
to get data from a URL:
fetch('https://example.com/data.json')
.then(response => response.json())
.then(data => {
console.log(data);
});
The fetch()
function returns a Promise that resolves to a Response
object. To get the data from the response, you can use the .json()
method, which also returns a Promise that resolves to the data in JSON format.
You can also use the await
keyword to wait for the Promise to resolve. Here’s an example of how to use await
with fetch()
:
async function getData() {
const response = await fetch('https://example.com/data.json');
const data = await response.json();
console.log(data);
}
getData();
Keep in mind that await
can only be used inside an async
function.
- 0
- By dummy.greenrakshaagroseva.com
11, Jan 2023
How to detect DNS lookup error in JavaScript

In JavaScript, you can use the XMLHttpRequest
object or the fetch()
function to send an HTTP or HTTPS request to a server, and handle the response. However, these methods do not provide a way to detect DNS lookup errors directly.
One way to detect a DNS lookup error is to check the status
code of the response, which should be 0 if the request failed due to a DNS error. Additionally, you can check the statusText
property, which will be “DNS Lookup Failed” if a DNS error occurred.
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if(xhr.status === 0 && xhr.statusText === "DNS Lookup Failed") {
console.log("DNS Lookup Failed");
} else {
console.log("Request successful");
}
}
};
xhr.send();
Another way is to use fetch
method.
fetch("http://example.com")
.then(response => {
if(!response.ok) {
console.log("DNS Lookup Failed");
}else {
console.log("Request successful")
}
})
.catch(error => console.log(error));
It is also important to note that different browser may have different way of handling this. In some cases browser may even not return anything, in such case you can set timeout to detect such failures.
9, Jan 2023
How do I make an HTTP request in Javascript?

There are several ways to make an HTTP request in JavaScript, and the method you choose will depend on the library you are using and the type of request you need to make. Here are some options you can use:
XMLHttpRequest: This is a built-in object in JavaScript that allows you to make HTTP requests from client-side JavaScript. You can use it to send an HTTP request to the server and load the server’s response data back into the script. Here’s an example of how to use it:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/url', true);
xhr.onload = function () {
// handle response data
};
xhr.send();
fetch: This is a modern way to make HTTP requests that is supported in all modern browsers. It uses a promise-based API and is easier to use than XMLHttpRequest. Here’s an example of how to use it:
fetch('/my/url')
.then(response => response.json())
.then(data => {
// handle response data
});
jQuery.ajax(): If you are using the jQuery library, you can use the $.ajax()
function to make an HTTP request. It has a lot of options and is very flexible, but it can be a bit more verbose than the other options. Here’s an example of how to use it:
$.ajax({
method: 'GET',
url: '/my/url',
success: function (data) {
// handle response data
}
});



