elisa tramonti elisa tramonti - 1 year ago 180
Javascript Question

Fetching in javascript with absolute url and relative url

I have a script that I run on my local server and that fetches a php file (on the local server too). If I write the url to fetch as a relative path, I get the file without problems, but, if I add the, I get a 403 error.

function localServerCall() {
var urlLocalServer = '';
//var urlLocalServer = 'myfile.php'; //THIS WORKS!
fetch(urlLocalServer).then(function(response) {
return response.json();
}).then(function(data) {
}).catch(function(err) {
console.log ('ERROR LOCALSERVER', err);

I was wondering if there are some limitations on the use of absolute/relative urls with fetch or if this problem might be due to something else.

Answer Source

A URL that doesn't start with a scheme or with // is treated as a path (part of a relative URL).

From http://example.com/foo/, your URL resolves to http://example.com/foo/

You almost certainly want:

var urlLocalServer = '';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download