Why can I not get a JSON file from the local network using a XMLHttpRequest? [duplicate]

StackOverflow https://stackoverflow.com/questions/23672273

  •  23-07-2023
  •  | 
  •  

Вопрос

I have found that many had similar problem:

XMLHttpRequest cannot load %3192.168.100.201:8080/history?_=1400139870373. Cross origin requests are only supported for HTTP.

I have tried to start the browser like:

--disable-web-security
--allow-file-access-from-files

How can I get the JSON file from server on local network?


Update

JS code:

function getHistory() {
        $.ajax({
            url: '192.168.100.201:8080/history',
            dataType: 'json',
            success: function(data) {
                console.log(data); 
            },
            cache: false
        });
     }
Это было полезно?

Решение

Cross origin requests are only supported for HTTP.

This is because you forgot http:// in your request:

url: 'http://192.168.100.201:8080/history',
  • The browser does not know what protocol you want.

You should also send an access control header (read about Cors [1]) from the targeted serverside:

Access-Control-Allow-Origin: http://<requesting host>

Not a must in every case, but a good practice and security feature by modern browsers.

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top