Access-Control-Allow-Origin error

  • 3
  • Problem
  • Updated 6 years ago
I have this error when I use ajax to retrieve data.

Origin http://mywebsite is not allowed by Access-Control-Allow-Origin.

If I open this ajax file directly from my browsers.
Safari has no problem. Chrome has the same problem.
Can the server enable CORS support?
Or is there any workaround?

thanks

Andy
Photo of AndyWu

AndyWu

  • 4 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 3
Photo of AndyWu

AndyWu

  • 4 Posts
  • 0 Reply Likes
I finally notice that I can use jsonp by appending ?callback=myCallbackFunction to the url.
Photo of Rudy J. Nickmann

Rudy J. Nickmann

  • 4 Posts
  • 0 Reply Likes
Hi,

Exactly what is "myCallBackFunction"?

I hate to sound so clueless on this matter. I wrote some php scripts that were successfully downloading weather conditions from Weather Underground personal weather station xml files. They suddenly failed several weeks ago. I learned that now I need a key and have to learn some basic JSon.

I use FireFox and Host Monster. I uploaded some very simple JSon code examples to the HostMonster server and they ran. However, when I cut and pasted the example php from the Weather Underground code samples...I get no output. I can get a response using APIGEE so I sure there is no problem with my user key.

{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
?>

Shouldn't the above code work? I copied it...called it JSonTest.php and uploaded it to my host server. This really driving me crazy!

Take it easy,

Rudy
Photo of Rudy J. Nickmann

Rudy J. Nickmann

  • 4 Posts
  • 0 Reply Likes
Hi,

I have tried to copy and past code into this text window three times and I notice that the code is truncated every time when I send it!

Take it easy,

Rudy
Photo of AndyWu

AndyWu

  • 4 Posts
  • 0 Reply Likes
Hi,
You should try JSONP.
This code should work.

<script>
function receiveData(data) {
if(data.response.error) {
do something;
} else {
do something else;
}
}
</script>
<script src="http://api.wunderground.com/api/yourKey/conditions/forecast/hourly/q/GA/Atlanta.json?callback=receiveData"></script>

Also, if you use chrome, look at the console in developer tools. You may see errors there.

Andy