danielm / uploader

A lightweight and very configurable jQuery plugin for file uploading using ajax(a sync); includes support for queues, progress tracking and drag and drop.
https://danielmg.org/demo/java-script/uploader
MIT License
1.17k stars 385 forks source link

Parse error #3

Closed devries123 closed 10 years ago

devries123 commented 10 years ago

Hello,

I'm trying to use your uploader, but I have a problem. I get it to show and work as long as I don't put anything more in the upload.php . Once I put my php code for uploading in it, it gives the following error:

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

I use the following code to upload the (multiple) files to the server:

$dir = 'test/'; if(is_uploaded_file($_FILES['files']['tmp_name'])){ $file = $_FILES['files']['name']; if(move_uploaded_file($_FILES['files']['tmp_name'], $dir.$file)) { echo json_encode(array('status' => 'ok')); } else { } }

Do you know how to fix this error?

danielm commented 10 years ago

Hey, Not sure, but my guess is that your PHP is returning an empty or invalid JSON. Try adding some errors to your code: Also if you are using the plugin with the default options (not using the fileName option) you may want to use $_FILES['file'] and not 'files'

$dir = 'test/';
if(is_uploaded_file($_FILES['file']['tmp_name'])){
  $file = $_FILES['file']['name'];
  if(move_uploaded_file($_FILES['file']['tmp_name'], $dir.$file))
  {
    echo json_encode(array('status' => 'ok'));
  }
  else
  {
    echo json_encode(array('status' => 'error', 'msg' => 'move_uploaded_file failed'));
  }
} else 
{
  echo json_encode(array('status' => 'error', 'msg' => 'is_uploaded_file failed'));
}

Cheers!

devries123 commented 10 years ago

Thank you, it is indeed working now!

zevero commented 8 years ago

Just tried it with nodejs and got a "Syntax Error", when returning just a string "ok".

The reason is, that I copied your example. And it uses JSON.stringify(data) which naturally throws an errror.

Returning a json object like {status:"ok"} or {status:"error", msg:"Error message"} helped ... Maybe you should state the server side requirements in the readme.

I know now... there are no requirements. It depends entirely on jquery... ... but if I had known this, I would have felt easier :)

Very nice plugin! Thank you very much to share it with us!