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