0

I am using Amcharts to create column charts which rotate and become horizontal bar charts. I am using dataLoader plugin of Amcharts to connect to a php file which is connecting to mysql and retreiving data and the php file is outputting data as JSON

Issue I am facing is that if the php file is in the same folder as the file containing the html, the chart loads properly

,
  "dataLoader": {
    "url": "data.php"
  },

But if I put the php file in a include folder, chart does not load and it gives a blank screen.

,
  "dataLoader": {
    "url": "http://MyWebsite.com/include/data.php"
  },

I checked the php json file both in the root and in the include folder and both are properly outputting valid JSON

I do not want to use Jquery and I am new to javascript. Please tell what I am doing wrong.

Update - This loading error is coming on and off. This is the error coming in the browser console - Failed to load http://MyWebsite.com/include/data.php: No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://www.MyWebsite.com is therefore not allowed access

user20152015
  • 344
  • 2
  • 23
  • Do you have any errors in your browser console? – xorspark Oct 26 '17 at 15:59
  • No. But I will try to check if for 1-2 days for any error – user20152015 Oct 26 '17 at 17:12
  • `Failed to load http://MyWebsite.com/include/data.php: No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://www.MyWebsite.com is therefore not allowed access` @xorspark This is the error which I am getting in browser console. – user20152015 Oct 27 '17 at 05:18
  • 1
    Maybe you should try `"/include/data.php"` avoiding the website address. – Darlesson Oct 27 '17 at 05:19
  • Check this link: https://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains. You would have this issue when you are accessing a different address. – Darlesson Oct 27 '17 at 05:22
  • @Darlesson What is the error saying in plain english ? – user20152015 Oct 27 '17 at 05:24
  • It's saying that for security reasons, a domain cannot access another domain straight from the browser. If you are using jQuery, check for JSONP at http://api.jquery.com/jQuery.ajax/. – Darlesson Oct 27 '17 at 05:40
  • @Darlesson, I do not know jquery and so do not use it – user20152015 Oct 27 '17 at 05:41
  • @Darlesson Since this problem used to come on and off, I have observed for some time and found that your fix works. So, you may put your fix as an answer and explain why the full url creates loading problems. And i will award that answer as being correct. If same problem repeats in future, I will comment on that answer. – user20152015 Oct 27 '17 at 12:48

1 Answers1

1

Due to the browsers' security, trying load content from a different address may result in a cross-origin error as explained here: Access-Control-Allow-Origin Multiple Origin Domains?. Like you mentioned, the difference between http:// and http://www for the same website is causing it to throw the error on and off.

Considering the call is actually for the same address, the best solution is to just change from http://MyWebsite.com/include/data.php to include/data.php

Darlesson
  • 5,742
  • 2
  • 21
  • 26