-1

I have a basic ajax request that retrieves a menu and puts it on a web page.

Here is the code

//display the menu
$.post(server+"quizz/phpScripts/retrieveMenu.php", {}, function(data){
    $("nav, #lowerMenu").html(data);
});

In my original files, 'server' was 'localhost', but when I hosted the site and tried to open the page, I get this error in the console.

Using firefox

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://edwaze.com/quizz/phpScripts/retrieveMenu.php. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Using Safari

XMLHttpRequest cannot load https://edwaze.com/quizz/phpScripts/retrieveMenu.php. Origin http://www.edwaze.com is not allowed by Access-Control-Allow-Origin.

Using Chrome

XMLHttpRequest cannot load https://edwaze.com/quizz/phpScripts/retrieveMenu.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://edwaze.com' is therefore not allowed access.

I've read quite a bit on this, but I haven't found a solution yet. Please help. And go easy with the terminologies please, i'm a bit new to ajax :)

Thanks.

Vincent Hokie
  • 61
  • 2
  • 8

2 Answers2

2

As you are not making any cross domain requests it would be better to use root relative urls:

$.post("/quizz/phpScripts/retrieveMenu.php" 

the leading / represents the root of the application.


That issue arises when you have http/https protocols added in the url and urls are hosted on webserver not on localhost.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • unfortunately, this didn't work guys..the errors are gone, but i still don't get the menu displaying – Vincent Hokie Jan 08 '16 at 14:21
  • @VincentHokie could you plz tell us what do you see in the network tab of your browser inspector tool? Do you see any request logged? – Jai Jan 08 '16 at 14:39
  • Sorry @Jai is appears your solution did work, my queries are being executed on the server end, my problem appears to now be the php version. So thank you. Do you happen to know where php version 5.4 native supports mysqli functions? – Vincent Hokie Jan 08 '16 at 14:56
0

Please go through this URL. It will help you to resolve your query.

CORS header 'Access-Control-Allow-Origin' missing

As per the errors, it seems that you need to set Cross Origin so that you can access data from another domain.

Community
  • 1
  • 1
Hemant Kabra
  • 870
  • 9
  • 29