blueimp / jQuery-File-Upload

File Upload widget with multiple file selection, drag&drop support, progress bar, validation and preview images, audio and video for jQuery. Supports cross-domain, chunked and resumable file uploads. Works with any server-side platform (Google App Engine, PHP, Python, Ruby on Rails, Java, etc.) that supports standard HTML form file uploads.
https://blueimp.github.io/jQuery-File-Upload/
MIT License
30.94k stars 7.95k forks source link

Error: SyntaxError: Unexpected token < #647

Closed Dacarts closed 12 years ago

Dacarts commented 13 years ago

Hi im trying to upload images files but everytime i push upload

i get this - Error: SyntaxError: Unexpected token <

pleas help and thx for any help.

Dacarts commented 13 years ago

i just run the test and i got these errors

Triggers destroy callback Passes over deletion url parameter Expected:
"test" Result: "" Diff:
"test" "" Source: at HTMLDivElement. () Passes over deletion request type parameter Expected:
"DELETE" Result: "" Diff:
"DELETE" "" Source: at HTMLDivElement. () Triggers destroy callback Passes over deletion url parameter Expected:
"test" Passes over deletion request type parameter Expected:
"DELETE" Expected 3 assertions, but 6 were run

but i don´t know what to look for to change that please advise ?

kadaji commented 13 years ago

Hi, I am getting the same error. Were you able to resolve it?

kadaji commented 13 years ago

Hi, try setting the display_errors to 0 in your upload.php. Also, there could be issues with your folder perm. I had to set mine to 777 (unsafe but that's what it took)

clark800 commented 12 years ago

I had this problem because I didn't have PHP installed.

ghost commented 12 years ago

I am also having this issue. I definitely have php installed, and kadaji's solution seems to be avoiding the issue rather than solving it (no offense meant). Any other solutions or clues?

coolguy982 commented 12 years ago

I just recently began having the "SyntaxError: Unexpected token <" error message. The strange thing is that it happens only when I upload large files (over 500 KB). When I upload ~50KB JPG files, they work just fine.

I am running this on an IIS server and am using the Classic ASP example code posted on this site with ASPUpload.

Any ideas?

katanacrimson commented 12 years ago

@coolguy982 Sounds like you're being given HTML instead of json. Dump the full response from the page, you might be hitting an upload limit or something.

coolguy982 commented 12 years ago

@damianb What is the easiest way to dump the full response from the page? Do I need to do this within a jquery upload js file?

ghost commented 12 years ago

I have the same issue , although I generate a valid json string and checked that at jsonlint.com.

ghost commented 12 years ago

This is a sample of json output :

[{"name":"29bbd4bf77c07c38c572a60c8547bd15_123646715_original.jpeg","size":36.05,"type":"image\/jpeg","url":"\/Users\/ams\/Code\/damlag\/elcinema\/src\/elcinema.com\/app\/public\/photos\/38\/123646715\/29bbd4bf77c07c38c572a60c8547bd15_123646715.jpg","thumbnail_url":"http:\/\/www.elcinema.ci\/photolist\/38\/29bbd4bf77c07c38c572a60c8547bd15_123646715_101.jpg","delete_url":"http:\/\/www.elcinema.ci\/backend\/upload\/delete_file\/123646715","delete_type":"DELETE"}]

hoona commented 12 years ago

I had a similar issue. Turns out that when I changed the "orient_image" option variable in the PHP UploadHandler class constructor from false to true, I was getting the infamous "SyntaxError: Unexpected token <." There seems to be an issue with the file_path. When I changed the "orient_image" option back to false, the erros went away.

It's weird, cause the unzipped package works fine untouched, but when I moved the UploadHandler class to the CodeIgniter library folder, changed the options, and moved some code into the controller, all of a sudden the error started popping up. I'm trying to find another spot where the file_path is defined, that I may have missed. Other than that, everything works fine.

hoona commented 12 years ago

Not sure why the example downloaded from github works, but the reason why I was getting the error is because when orient_image is set to true, the function called a php function exif_read_image(), which only supports exif data from JPG and TIFF files, NOT PNG, which is what I was using. So, when exif_read_data ran on my .png file, php was throwing a warning message, messing up the json response.

Anyway, not sure how useful the orient_image function is, but just keep that in mind. You'll want to add some code to execute the orient_image function only if the extensions are .jpg or .tiff.

Cheers!

MastaBaba commented 12 years ago

Setting error_reporting(0) resolved the error being returned, for me. But though the upload worked, a thumbnail copy was still not being created.

rob478 commented 12 years ago

I have the same problem.... what we can do ???

kadaji commented 12 years ago

It might be that the thumbnail wasn't generated after the upload - Check the folder perms. FYI, the thumbnail is stored in a different folder as the uploaded file.

On Wed, Feb 15, 2012 at 8:45 AM, rob478 < reply@reply.github.com

wrote:

I have the same problem.... what we can do ???


Reply to this email directly or view it on GitHub:

https://github.com/blueimp/jQuery-File-Upload/issues/647#issuecomment-3980357

rob478 commented 12 years ago

I'm on local so no problem of folder perms

kadaji commented 12 years ago

do you see a thumbnail generated in the folder?

On Wed, Feb 15, 2012 at 8:50 AM, rob478 < reply@reply.github.com

wrote:

I'm on local so no problem of folder perms


Reply to this email directly or view it on GitHub:

https://github.com/blueimp/jQuery-File-Upload/issues/647#issuecomment-3980453

rob478 commented 12 years ago

No, there is only the image on files.

kadaji commented 12 years ago

if you don't see the thumbnail in its folder, its either folder perm issues (doesn't matter if you are on local) or your php was compiled without the graphic lib.

Do the folder perms match for 'files' and 'thumbnails' ??

On Wed, Feb 15, 2012 at 8:57 AM, rob478 < reply@reply.github.com

wrote:

No, there is only the image on files.


Reply to this email directly or view it on GitHub:

https://github.com/blueimp/jQuery-File-Upload/issues/647#issuecomment-3980549

rob478 commented 12 years ago

The perms are the same. I have try on the server, I have the same error... It's possible that php was compiled without the graphic lib, How can I verify this ?

I have this : GD Support enabled GD Version bundled (2.0.34 compatible) FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.3.9 GIF Read Support enabled GIF Create Support enabled JPEG Support enabled libJPEG Version 6b PNG Support enabled libPNG Version 1.2.37 WBMP Support enabled XBM Support enabled

kadaji commented 12 years ago

phpinfo()

also you can debug the function create_scaled_image() in upload.php and see where its failing.

On Wed, Feb 15, 2012 at 9:07 AM, rob478 < reply@reply.github.com

wrote:

The perms are the same. I have try on the server, I have the same error... It's possible that php was compiled without the graphic lib, How can I verify this ?


Reply to this email directly or view it on GitHub:

https://github.com/blueimp/jQuery-File-Upload/issues/647#issuecomment-3980726

rob478 commented 12 years ago

How can I debug the function ?

atwskris commented 12 years ago

Hopefully this will help some one, however, reading this post it sounds like a variety of errors. This is what fixed this error for me.

Set the "post_max_size" and "upload_max_filesize" in the php.ini or using php_values in .htaccess file. ini_set doesn't work for php because it is executed too late, the file is already uploaded before the file reaches your script.

In this situation, the unexpected token is a html error, I came across this from testing as debugging was not giving me any results by echoing variables because the error was happening even before the script was loaded. To help speed this up you can also check your server error logs for apache and will tell you in there as well. You could also go as far as turning on your php errors to write to a file, if it is not already.

davidber commented 12 years ago

I was getting the same issue. Somehow, someway, the thumbnails directory has to be set to 777 not 755. Once I set it to 777, the error went away.

jakeedwards commented 12 years ago

Same issue. Unzipped and uploaded strait out of the box. In Chrome I get 'SyntaxError: Unexpected token <' after i start upload. In Firefox it says 'SyntaxError: JSON.parse: unexpected character' But firebug doesn't show an error. The thumbnail shows after I choose the file but disappears after 'start upload'

I've set the permissions to 777, and 755 I've set my php.ini to 'upload_max_filesize = 20000000' & 'post_max_size = 20000000'

In the test page I only get to 14. Callbacks: done (0, 1, 1)Rerun Running Callbacks: fail

Any suggestions?

newenth commented 12 years ago

Hi, i have the same probleme, i downloaded et uploaded it straigthaway but it didn't work.. i always have "SyntaxError: Unexpected token <" at the end of the upload.

I tryed to change the access of file and thumbmail but it still not work.

Someone fixed this problem?

Thanks

khammami commented 12 years ago

I had the same problem here, as solution i put "error_reporting(E_ALL | E_STRICT)" into a comment "//error_reporting(E_ALL | E_STRICT)" and it works fine no more "SyntaxError: Unexpected token <"

newenth commented 12 years ago

Still not work =/

khammami commented 12 years ago

i got this error "SyntaxError: Unexpected token <" because error report inject some html code into the JSON response

@newenth use firebug or wireshark to track the origin of your error.

newenth commented 12 years ago

"Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' on line 15"

Line 15 is "protected $options;" .. i don't understand =/

newenth commented 12 years ago

Oh gosh, i fixed it!

I created a .htaccess 0604 on the root of my FTP with: "SetEnv PHP_VER 5_TEST SetEnv REGISTER_GLOBALS 0"

and it works..

GREAT!

panakeny commented 12 years ago

Hola

Resulta que el modulo me estaba funcionando bien cuando lo registre en un hosting gratuito donde lo tenia alojado... decidi comprar uno y migre toda la pagina.

Ahora cuando le doy a agregar un archivo el funciona correctamente... le doy a cargar y efectivamente la barra de progreso va en aumento mientras se carga el archivo... pero cuando termina de cargar me muestra al lado de la barra un error "SyntaxError: Unexpected token <".

Alguien podria ayudarme? la verdad es que no he podido arreglarlo por mis propios medios...

GRACIAS DE ANTEMANO!

shadle10 commented 12 years ago

Thank you @khammami! I had this error and ended up using firebug to narrow the issue down to my upload.class.php script which caused the error.

edderleonardo commented 12 years ago

I was getting the same issue. Somehow, someway, the thumbnails directory has to be set to 777 not 755. Once I set it to 777, the error went away.

cmaunoury commented 12 years ago

Dear all,

I would just like to hide the error. I have integrated the plugin into an existing Druobox uploader php class and it works perfectly, except it throws me this "Syntax Error : Unexpected Token <".

I have searched for an extra "<" in almost all the documents in the plugin, but I feel this is useless, since I am not using the php server class or anything of that type.

Since everything works fine otherwise, I would just like to hide the error, but I still need to be able to display the "File Too Large" or "Wrong Format" message, so I can't hide all errors with CSS.

Any idea?

jsheedy commented 12 years ago

I was having an issue with this in running the demo app. The problem was non-existant or incorrect permissions on the files/ and thumbnails/ directories. Firebug as mentioned above would work well for tracing the problem if you're still having it. You can also use google Chrome this way:

This should show you the HTML error messages being returned which cause the "Unexpected Token <" problem.

cmaunoury commented 12 years ago

Thanks a lot for your help, but unfortunately, when I try to debug this both in Firebug and with the Chrome Developper's tools, no error shows up. As I said, since I have inserted a PHP class that uploads files directly to dropbox, I don't think the php code from the Jquery Upload app is being triggered... At least there is no /server/php script triggered in the network tab in the Chrome Developper's Tools. I was thinking this error was maybe only coming from the JSON message that's being output after the upload and that it may be at least hidden or changed with some JSON manipulation. But I don't really understand how this works, so I am a bit at loss to do so...

Is what I am saying making any sense? Any idea about how to hide this error message?

As I also said, the script works perfectly otherwise...

cmaunoury commented 12 years ago

Not to clutter the discussion, but to be more precise, my idea here (which is obviously a dirty fix, but I cannot access the permissions of the dropbox) would be to be able to "grab" the specific "Syntax Error : Unexpected Token <" in the "errors" settings of the locale.js file so that instead of outputting this error, it would say "Your File Has Been Correctly Uploaded".

I tried with "syntaxError": "Your File has been successfully uploaded", and "SyntaxError", but without success. Of course, since I cannot display the server side error in any debugging platform, it makes it much harder...

jsheedy commented 12 years ago

The problem is that the server, your PHP class in your case, /server/php/index.php in my default case, is returning HTML error messages which the javascript client is trying to parse as JSON. The php server probably should return JSON errors instead. Using the developer tools is a way to track the current problem down to fix it.

blueimp commented 12 years ago

Please read: https://github.com/blueimp/jQuery-File-Upload#support

honavarsohaan commented 11 years ago

oops my bad @chriscct7 i didn't read your comment i was so happy that i solved the problem that i posted here without reading your comment ...

fahadbillah commented 11 years ago

@jsheedy you saved me man..... much appreciated........

farouk-mokrani commented 11 years ago

i have a same error Error SyntaxError: Unexpected token < any solution please ?

edwormald commented 11 years ago

I had this error too (Error SyntaxError: Unexpected token <). This was because I had copied the main libraries into a different folder and put the uploader html file (the main view) somewhere else. When I included the main.js in this file it expectec to find the server side script (php) at /server/php/ and couldnt. So just needed to correct the path on line 22 of main.js so that it could post successfully to the script.

artsmorgan commented 11 years ago

i had the same error too, it was fixed changing directory permissions.

StephanWagner commented 11 years ago

If you came this far and you still have the error, this is how I fixed it:

Seems like you need at least PHP Version 5.3. If you have 5.2 you will get a warning (if you haven't disabled errors in PHP) from Line 250 in file UploadHandler.php. This will stop the headers for creating the file to be sent.

So disabling error messages should work, as mentioned several times above, but if you still want other errors to show, change Line 250 in UploadHandler.php to

@clearstatcache(true, $file_path);

The @ stopps the error to display. And as mentioned, its just a warning so your files will still process correctly. At least mine did :)

Another thing is debugging. It took me a while to figure out how to do it. The post of @jsheedy's explains it quite good:

You can also use google Chrome this way:

  • open the developer tools by right clicking the page and selecting "Inspect Element"
  • click network tab
  • in the column labelled "name", click on /server/php
  • in the submenu, click response

If you don't see any files there, reload the page while Chromes debugging tool is open and on the network tab. Its not always labelled /server/php though, in my case the filename was different, but you'll find it!

pepeortiz commented 11 years ago

This is how I fixed it... I found an alternative to the "json_encode()" function here:

http://www.dzone.com/snippets/jsonencode-alternative-php-4

I added the function to UploadHandler.php prior to calling:

$json = json_encode($content);

After that the error disappeared because it is now able to read/parse the json response.

HTH

jquellier commented 10 years ago

Hi,

I encounter the following error: "Error : SyntaxError : Unexpected token <" when I download a file 1.5MB This is a JSON error because it attempts to interpret an HTML error:

Here is the HTML problematic issue :


Fatal error </ b>: Out of memory ( Allocated 27262976 ) ( tried to allocate 3888 bytes) in / homepages/1/d265822930/htdocs/immoxo/jQueryUpload/server/php/UploadHandler.php </ b> on line 492 </ b >

And here is the line 492 in the "UploadHandler.php" file :

  1. protected function gd_get_image_object ($ file_path , $ func, $ no_cache = false) {
  2. if ( empty ($ this-> image_objects [$ file_path ] ) | | $ no_cache ) {
  3. $ this-> gd_destroy_image_object ($ file_path ) ;
  4. $ this-> image_objects [$ file_path ] = $ func ( $ file_path ) ;
  5. }
  6. return $ this-> image_objects [$ file_path ] ;

495. }

All directories have the rights ( 777 ) here is my phpinfo() parameters : memory_limit = 90M post_max_size = 8M upload_max_filesize = 40M

I do not understand where the problem is ... Can you help me?

thank you very much

creativedata commented 10 years ago

This does the trick:

Add 'index.php' to line 21 of main.js:

url: 'server/php/index.php'

AnithaSudhagar commented 10 years ago

when i see the developer tools it shows, "statuscode:302 not modified",,how to solve it