Uncaught SyntaxError: Unexpected token : (JSON)


#1

Screenshot : https://imgur.com/a/zFu8v

I keep getting this error when making a $.getJSON request, it looks like a problem with the api, not necessarily my code.

Here is my code:


#2

This is hard to debug as a screenshot.

First point CORS requires you to make a JSONP request when you are doing cross domain operations. Please check that your request is using the proper format i.e. callback= as it appears to not have that in this example.

Here is a working Glitch demo you can experiment with. If you look inside with the remix option you will see that the JS components are making local calls to a server side node server that then sends the request to the Solcast API. This is one way to avoid CORS issues without using jsonp. Make sure to set the .env file with your Solcast API key. Also as this is glitch you can ask for help with syntax/logic help by the Ask the Community for Help option.

image

The screenshot is helpful for context, but the actual code is what is needed to get a good idea of what is happening for you.

If you would prefer a different method to debugging your issue may I suggest creating a JSFiddle or GitHub gist that presents the actual issue you are having.


#3

Hei man, i tried by myself to fix this but the problem is still the same, here is my project on github : [REMOVED]

When you type an address on the address bar, make sure you type “Street/City/Country” eg: “Ugo Bassi street, Bologna, Italy”


#4

First of all your sensitive API keys are in public so I would recommend hiding them through this process please https://blog.github.com/2013-01-25-secrets-in-the-code/

Try using a proxy service. That is the idea I said before with glitch, but I have submitted a GitHub Proxy service code to your repository that uses the Heroku open CORS proxy service for you.

	function useCoordinates(lat, lon){
		const proxyurl = "https://cors-anywhere.herokuapp.com/";
		const url = 'https://api.solcast.com.au/radiation/forecasts?format=json&latitude=' + lat +'&longitude='+ lon +'&api_key=SOLCAST_API_KEY'; // site that doesn’t send Access-Control-*
		fetch(proxyurl + url)
		.then(response => response.text())
		.then(contents => alert(contents))
		.catch(() => console.log("Can’t access " + url + " response. Blocked by browser?"))
	}

#5

Thank you so much for your help, i’ll also hide my sensible data.
One last thing: do you know how can i request radiation data of the past week? Or the past month? Asking radiation data of the past 30 minutes is not that useful, while having an average of the radiation of the past week/month would be better for my project


#6

You can get the last week radiation data with the estimated_actuals radiation call

Radiation Estimated Actuals API

https://api.solcast.com.au/radiation/estimated_actuals?longitude=149.117&latitude=-35.277&api_key=<YOUR_KEY>&format=json

To get the latest values use the latest call below

https://api.solcast.com.au/radiation/estimated_actuals/latest?longitude=149.117&latitude=-35.277&api_key=<YOUR_KEY>&format=json

If you want to get a months worth of data you will need to call the API once a week for at least 3 weeks and store the values as this is a beta the extended query support is being worked on.