0

I am using a croppie library to help me cropp a picture. However the strange part is that sometimes the croppie works and sometimes it is unable to cropp the picture. When it works I do not get any errors in the console but when it does not work I get this error in the console enter image description here

This is the croppie librarry I have downloaded and are currently using https://github.com/fengyuanchen/cropperjs

Here is my code

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="css/Profil.css"> <!-- Länk till CSS -->
  <link rel="stylesheet" href="css/cropper.css"> <!-- Cropper -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" crossorigin="anonymous"> <!-- Cropper -->
  <script src="https://code.jquery.com/jquery-3.4.1.min.js" crossorigin="anonymous"></script> <!-- Croppper -->
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <!-- Croppper -->
  <script src="js/cropper.js"></script> <!-- Croppper -->

        <div class="container">
            <label class="label" data-toggle="tooltip" title="Change your avatar">
              <img class="circle-image" id="avatar" src="https://avatars0.githubusercontent.com/u/3456749?s=160" alt="avatar">
              <input type="file" class="sr-only" id="input" name="image" accept="image/*">
            </label>
          <!-- <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
            </div> -->
            <div class="alert" role="alert"></div>
            <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="modalLabel">Crop the image</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <div class="img-container">
                      <img id="image" src="https://avatars0.githubusercontent.com/u/3456749">
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary" id="crop">Crop</button>
                  </div>
                </div>
              </div>
            </div>
          </div>

CSS

.label {
  cursor: pointer;
  margin-left: 35%;
  margin-top: 5%;
}

/* .progress {
  display: none;
  margin-bottom: 1rem;
  width: 28%;
  margin-left: 35%;
} */

.alert {
  display: none;
  width: 28%;
  margin-left: 35%;
  text-align: center;
}

.img-container img {
  max-width: 100%;
}

.circle-image {
  border-radius: 50%; 
}

.cropper-view-box {
  border-radius: 50% !important;
}

.cropper-dashed {
  border: none !important;
}

Javascript

window.addEventListener('DOMContentLoaded', function () {
      var avatar = document.getElementById('avatar');
      var image = document.getElementById('image');
      var input = document.getElementById('input');
      var $progress = $('.progress');
      var $progressBar = $('.progress-bar');
      var $alert = $('.alert');
      var $modal = $('#modal');
      var cropper;

      $('[data-toggle="tooltip"]').tooltip();

      input.addEventListener('change', function (e) {
        var files = e.target.files;
        var done = function (url) {
          input.value = '';
          image.src = url;
          $alert.hide();
          $modal.modal('show');
        };
        var reader;
        var file;
        var url;

        if (files && files.length > 0) {
          file = files[0];

          if (URL) {
            done(URL.createObjectURL(file));
          } else if (FileReader) {
            reader = new FileReader();
            reader.onload = function (e) {
              done(reader.result);
            };
            reader.readAsDataURL(file);
          }
        }
      });

      $modal.on('shown.bs.modal', function () {
        cropper = new Cropper(image, {
          aspectRatio: 1,
          viewMode: 2,


        });
      }).on('hidden.bs.modal', function () {
        cropper.destroy();
        cropper = null;
      });

      document.getElementById('crop').addEventListener('click', function () {
        var initialAvatarURL;
        var canvas;

        $modal.modal('hide');

        if (cropper) {
          canvas = cropper.getCroppedCanvas({
            width: 160,
            height: 160,

          });

          initialAvatarURL = avatar.src;
          avatar.src = canvas.toDataURL();
          $progress.show();
          $alert.removeClass('alert-success alert-warning');
          canvas.toBlob(function (blob) {
            var formData = new FormData();

            formData.append('avatar', blob, 'avatar.jpg');
            $.ajax('https://jsonplaceholder.typicode.com/posts', {
              method: 'POST',
              data: formData,
              processData: false,
              contentType: false,

              xhr: function () {
                var xhr = new XMLHttpRequest();

                xhr.upload.onprogress = function (e) {
                  var percent = '0';
                  var percentage = '0%';

                  if (e.lengthComputable) {
                    percent = Math.round((e.loaded / e.total) * 100);
                    percentage = percent + '%';
                    $progressBar.width(percentage).attr('aria-valuenow', percent).text(percentage);
                  }
                };

                return xhr;
              },

               success: function () {
                $alert.show().addClass('alert-success').text('Upload success');
               },

              error: function () {
                avatar.src = initialAvatarURL;
                $alert.show().addClass('alert-warning').text('Upload error');
              },

              complete: function () {
                $progress.hide();
              },
            });
          });
        }
      });
    });
chandu komati
  • 795
  • 1
  • 5
  • 21
  • It's very odd that this is a intermittent problem. CORS headers are not a transient thing. Normally I'd close this as a dupe of [this question](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-get-a-no-access-control-allow-origin-header-is-pr) (which it may be worth your time to read so you understand what CORS is), however this is not a constant issue I'll leave it as there may be an underlying problem. I'd assume this is out of your control though, and is an issue on the receiving domain. Assuming you have an SLA with them, I'd raise a support request. – Rory McCrossan Dec 04 '19 at 10:15

1 Answers1

0

use live server

install live-server: npm install -g live-server
Move your terminal to where your pages live: cd <path-to-content>
Start the server: live-server .
Open localhost:8080 in a browser.
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33