Problem Getting Json file from wordpress server with Jquery getJson or ajax method

Problem Getting Json file from wordpress server with Jquery getJson or ajax method
0.0 0

#1

Hello fellow campers!

I am trying to do a simple ajax request to retrieve a json file from the server and store it in a javascript object.

I have tried 2 different methods .ajax() method and .getJSON().
Both methods in the Network console seem to fetch the file with result HTTP/1.1 200 OK.

But when I tried to output the received data in the console with console.log(data) nothing shows up.

I have enabled the cross-origin requests in Firefox as I run my code locally and the web server is on a Rpi(Wordpress).

$.ajax({
			url: 'http://192.168.0.227/json/sysC01.json',
			dataType: 'json',
			type: 'GET',
			cache: false,
			crossDomain: true,
			success: function(data){
				console.log(data);
			},
		
		});


$.getJSON("http://192.168.0.227/json/sysC01.json", function(result){
				console.log(result);
			});

I am logged in in the site of course. Maybe there is something in the server-side? I just stored the json files in a folder in the /var/www/html directory. If i request the json file from the url it shows as expected in the browser. I also temporarily disabled the antivirus in case it blocked the insecure communication.

As a first step I want to see that the data are received, which I can’t at the moment, and afterwards I would like to store the data in a Javascript object and output them inside a div element with a certain format.

Any suggestion is highly appreciated.


#2

The only times I have done similar to this I have used the file system to read the file.
For example:

function readTextFile(file) {
    var rawFile = new XMLHttpRequest();
    var allText = ''
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                allText = rawFile.responseText;
            }
        }
    }
    rawFile.send(null);
    // var allText = rawFile.responseText;
    return allText;
}

I also did a similar thing with a node project:


#3

Dear JohnnyBizzel,

Thanks a lot for your reply.
After trying a lot of different things, this was the one that worked:

I started chrome with web security disabled(it was not a Jquery issue).

chrome.exe --user-data-dir=“C:/Chrome dev session” --disable-web-security