Home javascript Ajax request failed - parsererror

Ajax request failed – parsererror

Author

Date

Category

We have a small html code:

& lt; input type = "file" multiple = "multiple" accept = "image / *" & gt;
& lt; a href = "#" class = "submit button" & gt; Upload files & lt; / a & gt;

I send collected images for processing via ajax

let files;
$ ('input [type = file]'). change (function () {
  files = this.files;
  if (files.length == 8) {
    $ ('. erroResponse'). hide ();
  } else {
    $ ('. erroResponse'). show ();
  }
});
$ ('. submit.button'). click (function (event) {
  event.stopPropagation (); // Stop what is happening
  event.preventDefault (); // Completely stop what is happening
  // Supply the form data and add the file data from files to it
  var data = new FormData ();
  $ .each (files, function (key, value) {
    data.append (key, value);
  });
  // Send the request
  $ .ajax ({
    url: '/acces/default/loadimage.php',
    type: 'POST',
    data: data,
    cache: false,
    contentType: false,
    processData: false,
    dataType: "json",
    success: function (respond, textStatus, jqXHR) {
      if (typeof respond.error === 'undefined') {
        // Files uploaded successfully, do something here
        str = respond.replace (/ \] \ [/ g, ",");
        var data = JSON.parse (str);
        console.log (data);
      }
      else {
        console.log ('server RESPONSE ERRORS:' + respond.error);
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log ('AJAX request ERRORS:' + textStatus);
    }
  });
});

As a result, I get an error: parsererror;
dataType: change to jsonp – same error;
dataType: change to text and get another error –

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Answer 1, authority 100%

Remove the line

dataType: "json",

or replace it with

dataType: "text",

Your response from the server does not match the JSON format.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions