CORS Policy - Change


#1

Hey guys

I’m trying to make a comment box for my own website, and I’m using the code from this website: https://codepen.io/anon/pen/zMNzxj/
It works on the link, but not on my own site, as an error in the console comes up stating this:

Access to XMLHttpRequest at ‘file:///Users/Amanda/Desktop/commentbox/comment.html’ from origin ‘null’ has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
send @ jquery.min.js:2
ajax @ jquery.min.js:2
(anonymous) @ comment.js:33
dispatch @ jquery.min.js:2
y.handle @ jquery.min.js:2

I have inserted the following in my HTML

Can you please help me? I think I need to change file:// to http://, but I don’t know how


#2
$($submit).click(function() {
    if ($text.html() == "") {
      alert("Plesea write a comment!");
      $text.focus();
    } else {
      _data = $text.html();
      $.ajax({
        type: "POST",
        url: window.local,
        data: _data,
        cache: false,
        success: function(html) {
          $loading.show().fadeOut(300);
          $listComment.append("<div>" + _data + "</div>");
          $text.html("");
          $totalCom.text($(".list-comments > div").length);
        }
      });
      return false;
    }
  });
});

In the onclick listener there is a ajax request. This go’s to window.local as can be seen in the url. this has to go to where you want to save the posts.

i know this just tells you where the problem is and not how to fix them. But writing something to file or database aint something easy to explain or fix.

if you do not care about the database functionality than you can strip the code from the succes:function out of the ajax request.

Youre .onclick function will look like this.

$($submit).click(function() {
    if ($text.html() == "") {
      alert("Plesea write a comment!");
      $text.focus();
    } else {
      _data = $text.html();
	  
	  $loading.show().fadeOut(300);
      $listComment.append("<div>" + _data + "</div>");
      $text.html("");
      $totalCom.text($(".list-comments > div").length);
	  
	  
      //$.ajax({
      //  type: "POST",
      //  url: window.local,
      //  data: _data,
      //  cache: false,
      //  success: function(html) {
      //      removed code outsire ajax
      //  }
      //});
      return false;
    }
  });
});