16

I am following a book example hence the code is very simple.

This is the code:

jQuery.get("ajax_search_results.php",
           { s:search_query },
           write_results_to_page, "html");

And the code is ajax_search_results.php has:

<div id="ajax_search_results">
 <ul>
 <li><a href="#">First result</a></li>
 <li><a href="#">Second result</a></li>
 <li><a href="#">Third result</a></li>
 <li><a href="#">More...</a></li>
 </ul>
</div>

This works as expected in the Firefox. But in Chrome, I can see the error following message in the JS console:

XMLHttpRequest cannot load 
file:///C:/xampp/xampp/htdocs/snk/ajax_search_results.php?s=keyword. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access.

Can you please help identify the problem?

Sklivvz
  • 30,601
  • 24
  • 116
  • 172
  • 3
    Open the file via your web server, eg `http://localhost/someFile.html`. Chrome doesn't allow AJAX requests for `file:///` URLs by default and your PHP script wouldn't execute anyway – Phil Jan 09 '14 at 05:46
  • Do you open this as a file? open it on server, which means the address is like http://.... – Andrew Jan 09 '14 at 05:47

3 Answers3

5

If your problem is like the following while using Google Chrome:

[XMLHttpRequest cannot load file. Received an invalid response. Origin 'null' is therefore not allowed access.]

Then create a batch file by following these steps:

Open notepad in Desktop.

  1. Just copy and paste the followings in your currently opened notepad file:

start "chrome" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files exit

  1. Note: In the previous line, Replace the full absolute address with your location of chrome installation. [To find it...Right click your short cut of chrome.exe link or icon and Click on Properties and copy-paste the target link][Remember : start to files in one line, & exit in another line by pressing enter]
  2. Save the file as fileName.bat [Very important: .bat]
  3. If you want to change the file later then right-click on the .bat file and click on edit. After modifying, save the file.

This will do what? It will open Chrome.exe with file access. Now, from any location in your computer, browse your html files with Google Chrome. I hope this will solve the XMLHttpRequest problem.

Keep in mind : Just use the shortcut bat file to open Chrome when you require it. Tell me if it solves your problem. I had a similar problem and I solved it in this way. Thanks.

Sakib
  • 51
  • 1
  • 1
  • 3
    Note: One should use this **only** for local testing due to security reason. http://www.chrome-allow-file-access-from-file.com/ – AbcAeffchen Sep 06 '14 at 13:21
4

add this at the top of file,

header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
Kishan_KP
  • 4,488
  • 6
  • 27
  • 46
  • 3
    Simply deactivating CORS is NOT an elegant solution. Do not do this. Better to understand what CORS is and what to do with it: http://www.html5rocks.com/en/tutorials/cors/ – Felipe Caldas May 25 '16 at 03:16
1

This is supposedly because you trying to make cross-domain request, or something that is clarified as it. You could try adding header('Access-Control-Allow-Origin: *'); to the requested file.

Also, such problem is sometimes occurs on server-sent events implementation in case of using event-source or XHR polling in IE 8-10 (which confused me first time).

Damaged Organic
  • 8,175
  • 6
  • 58
  • 84