1

Accessing data.json file using ajax calls receiving error as

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

I am using a local json file, not server data.

I have tried with start chrome --allow-file-access-from-files is is working fine but everytime i need to run these command

How to add metatag for allow-file-access-from-files for particular file when code runs in chrome/jquery

Ajax call :

 $.ajax({
        url: 'data.json',
        dataType: 'JSONP',
        jsonpCallback: 'callbackFnc',
        type: 'GET',
        async: false,
        crossDomain: true,
        success: function (data) {
            engines = data;
            myDOM();
         },
        failure: function () { },
        complete: function (data) {
            if (data.readyState == '4' && data.status == '200') {
                console.log(" Status: 'SUCCESS' ");
            }
            else {
                console.log(" Status: 'FAIL' ");                   
            }
        }
    });

data.json :

[
    {
        "Function": "CAB",
        "Part_No": "RE284091",
        "Description": "Cab Air Filter",
        "Hours": "1000",
        "Model": "7200R",
        "Serial_NO":"xxxx"
    },
    {
        "Function": "CAB",
        "Part_No": "RE291412",
        "Description": "RECIRCULATION AIR FILTER",
        "Hours": "1000",
        "Model": "7200R",
        "Serial_NO":"xxxx"
    }

]

TsTiX
  • 375
  • 2
  • 5
  • Welcome on StackOverflow. Please have a look at [Markdown help](https://stackoverflow.com/editing-help) to improve the readability of your next question. – Andreas Apr 19 '18 at 06:12
  • 1
    Possible duplicate of ["Cross origin requests are only supported for HTTP." error when loading a local file](https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local) – Andreas Apr 19 '18 at 06:14
  • Why don't you run a local server? – connexo Apr 19 '18 at 07:24

2 Answers2

0

have you tried this

$.ajax({
    url: 'http://data.json',
    dataType: 'JSON',
    type: 'GET',
    async: false,
    crossDomain: true,
    success: function () { },
    failure: function () { },
});

if it doesnt work try to use JSONP , since it would bypass the same origin policy

$.ajax({
        url: 'pathtofile',
        dataType: 'JSONP',
        jsonpCallback: 'callbackFnc',
        type: 'GET',
        async: false,
        crossDomain: true,
        success: function () { },
        failure: function () { },
        complete: function (data) {
            if (data.readyState == '4' && data.status == '200') {
                errorLog.push({ IP: Host, Status: 'SUCCESS' })
            }
            else {
                errorLog.push({ IP: Host, Status: 'FAIL' })
            }
        }
    });
Justinjs
  • 130
  • 7
0

You should try and run it through a webserver. The browser itself shall not be able to access files due to security concerns. You should also not try and bypass that by setting some browser-specific flags.

I had a similar issue and running it inside a web-server fixed it for me.

TsTiX
  • 375
  • 2
  • 5