Ajax Cross origin requests are only supported for protocol schemes

This simple piece of code is not working, what should I do now

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ajax Tutorials</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"
                $('#test').load("data.txt", {
                    Name: "Vijesh",
                    Lastname: "Arora",
                }, function(){
                    alert("Hi idiot!");

    <div id="test"><p>This is the first content!</p></div>
    <button id="btn">Click to change</button>

I have created the data.txt file in the same directory where this index.html is placed.

XMLHttpRequest cannot load file:///Applications/XAMPP/xamppfiles/htdocs/php/ajax/data.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

You need to run a local web server to use AJAX. You can’t do it by simply opening an HTML file in your browser.

If you’re on Windows, the easiest way is to use a packaged solution like XAMPP.

If you’re a little more tech savvy, then running you could install NGINX and just have it pointed at your projects directory. It uses <1mb of RAM and virtually no processor while idle.

Or to rephrase, you are not allowed to use “file:///” for cross-origin requests. It has to be “http://” or similar.

1 Like

Well to be fair, his code isn’t using either. :stuck_out_tongue_closed_eyes:

Doesn’t the browser use the same file:/// reference as that of which the HTML file was loaded?

$('#test').load("data.txt", {

Oh right, I see what you’re saying. Good point.

I am still using XAMPP server but no doubt this error was still appears.

You have to actually open that page from your localhost then, not from the file system. So if it’s in /Applications/XAMPP/xamppfiles/htdocs/php/ajax/ and XAMPP is running, the address would be something like (adjust the :port and name of the .html file).

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.