Using Axios as a Ping [duplicate] - javascript

This question already has answers here:
How to check if the user is online using javascript or any library?
(11 answers)
Closed 5 years ago.
I have a web page is running on localhost. This web page has some JavaScript that runs when the page is loaded. I want to see if the web page can connect to the external internet.
I thought I would use Axios, to see if I could hit Google. So, I tried the following:
axios.get('https://www.google.com')
.then(function (res) {
alert('Google found!');
})
.catch(function (err) {
console.log(err);
alert('Cannot find Google');
})
;
When this code runs, I see the following error in my console window:
XMLHttpRequest cannot load https://www.google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
My question is, how can I use Axios to see if I can reach the internet?

Axios cannot make cross domain requests, You can try using a cross domain library.
For example, jquery, zepto and so on.

Related

Fetch API won't fetch certain websites [duplicate]

This question already has answers here:
Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
(13 answers)
No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
(26 answers)
Closed 14 days ago.
I am trying to access a JSON from the NYT from this URL: https://www.nytimes.com/svc/wordle/v2/2023-01-30.json
This is my first time using fetch, but whenever I run the following code...
fetch('https://www.nytimes.com/svc/wordle/v2/2023-01-30.json')
.then(response => response.json())
.then(data => {
console.log(data)
});
I get: "TypeError: Failed to Fetch". I am using Replit to run this code, but using this URL (https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1) from someone else's example runs perfectly fine in the same piece of code.
I've tried adding await in case the function was taking too long, as well as other forms of HTTP requests. They work when using the deckofcards URL, but not the NYT one.
Is there a specific parameter I need to add for this particular URL? I appreciate any pointers!

API working with POSTMAN but not with fetch [duplicate]

This question already has an answer here:
Fetching API json data alert: Content Security Policy: The page’s settings blocked the loading of a resource at
(1 answer)
Closed 4 months ago.
I know there have been lots of questions on this topic and I read them all but not able to resolve my issue so asking here.
I am getting the following error while trying to access an api using fetch-
Refused to connect to 'https://xxxxx/api' because it violates the
following Content Security Policy directive: "connect-src 'self'
updates.developer.mozilla.org
www.google-analytics.com stats.g.doubleclick.net"
I don't have any control over this api so I can't add anything to connect-src. When making a fetch(api_url) request I am getting the above error but while making the same GET request from POSTMAN(or directly from url bar of browser) then I am able to receive the response body(which is an xml).
Also while making a fetch request with mode set to 'no-cors' I am able to get a response but not the response body. I want the response body.
What is POSTMAN doing differently? How can I achieve the same using fetch()?
Try installing the 'CORS Unblock' chrome extension and then run your application. Also, this error generally occurs when we use an API link as http://'apilink' when the link is https://'apilink' or vice-versa. Please check that as well.

Fail to load JSON file due to no 'Access-Control-Allow-Origin' is present on the requested resource [duplicate]

This question already has answers here:
Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
(13 answers)
Closed 5 years ago.
I've been trying to get data from a JSON file that's in a Github Repo. I'm using just XMLHttpRequest().
$(function() {
load();
function load() {
var fetch = new XMLHttpRequest();
fetch.open(
"GET",
"https://github.com/prvnbist/Periodic-Elements-App-Using-JSON-And-JQuery/blob/master/elements.json",
true
);
fetch.onload = function() {
if (this.status == 200) {
var elem = JSON.parse(this.responseText);`
}
}
}
});
This is the error I'm getting!
Failed to load https://github.com/prvnbist/Periodic-Elements-App-Using-JSON-And-JQuery/blob/master/elements.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://s.codepen.io' is therefore not allowed access.
The code works perfectly on localhost, ofcourse but on codepen it's giving me this error which is legit for security purposes but I haven't been able to get around it.
Here's the link to Codepen - https://codepen.io/prvnbist/pen/EwOapM
You are running into the same origin policy, and the browser is suggesting using CORS to securely access GitHub. But you don't have access to GitHubs servers to make that change.
GitHub is not an API, and thus does not implement the CORS headers. The workaround is to use a proxy service like RawGit to access your files.

How can I obey access control origin exception while querying data from different host in node.js/angular app? [duplicate]

This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 7 years ago.
There is a webpage http://ip2c.org/ and it parses any ip number to a country name, all I have to do is to give it an argument such as http://ip2c.org/10.0.1.3 (I know it's my local host, it's just for tests). When I run it in the browser, I'm seeing 1;ZZ;ZZZ;Reserved which is fine, I understand it's not an Ip address that can be reversed-geo tagged. But I expected to have a similar result in my node.js/angular app.
So far I created in my backend code a function:
app.get('/get/ip/address', function (req, res) {
var ip = require('ip');
res.json(ip.address());
})
and in my front end query controller:
$scope.countryName = function() {
$http.get('/get/ip/address').success(function (data) {
console.log(data);
console.log("!!!!!");
$http.get('http://ip2c.org/'+data).success(function (data) {
console.log(data);
})
})
}
When I run it, I see:
queryCtrl.js:127 10.0.1.3 queryCtrl.js:128 !!!!! webpage.html:1
XMLHttpRequest cannot load http://ip2c.org/10.0.1.3. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:3000' is therefore not allowed
access
How can I obey it and see the correct result in my console?
It is the server that decides what browsers can query it and whether a cross origin request is allowed or not. Your client-side angular code cannot change/fix that in any way. If the server isn't allowing a cross-origin request from the browser, then the browser just can't make that cross-origin request.
What you can do is move the functionality to your server. Here's how the sequence would work:
Client queries your server asking for country.
Your server gets the client IP address from that request.
Your server then makes a request to the ip2c.org URL to fetch the country.
Your server then returns that country to the angular client.
Since servers are not subject to cross-origin limitations (that is only a browser thing), you can have your server make the cross-origin request on your behalf.

How to get html/text from webpage [duplicate]

This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 8 years ago.
I'd like to get the text on this page:
https://cvo-v025.cvo-zwfryslan.nl/display/ToonBerichten.aspx?uid=ctl14&pid=723df4e4-248f-4df6-b3ad-751b410daab7&id=1c76d69d-d858-44d9-8a47-e65e9f294898
Php cUrl isn't working, YQL isn't working (but didn't give an error), javascript didn't work, the error message was:
XMLHttpRequest cannot load https://cvo-v025.cvo-zwfryslan.nl/display/ToonBerichten.aspx?uid=ctl14&pid=…3df4e4-248f-4df6-b3ad-751b410daab7&id=1c76d69d-d858-44d9-8a47-e65e9f294898. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://--------.nl' is therefore not allowed access.
Is there a way for me to get the text from that page?
The page you're trying to fetch text from actually makes a POST request to another resource to fetch the text via XHR. When you open Firebug or similar, you should see the POST request, its URL and response. It would appear that you need to have a session on the site to actually fetch anything, as making a POST request to that URL fails to retrieve anything useful.
Copying the request as CURL does yield a working terminal command, along with all sent headers, but I doubt it will be helpful if you wish to do this programmatically.

Categories