1

I have installed the jQuery file upload plugin. I have edited the upload handler file to insert the image name at database. Everything works fine but I got this error:

SyntaxError: Unexpected token < in JSON at position 0

After I refresh the page I see the file moved to the correct position and database updated all OK, but keep getting this error every time I upload something.

Here is the code I inserted to options array for database connection:

'database' => 'easywp_casino',  
'host' => 'localhost',  
'username' => 'root',  
'password' => '',

This code I insert in handle_file_upload function:

$file->upload_to_db = $this->add_img($name);

and this is the code for database image name insert which I add after the handle_file_upload function:

function query($query) {  
    $database = $this->options['database'];  
    $host = $this->options['host'];  
    $username = $this->options['username'];  
    $password = $this->options['password'];  
    $link = mysql_connect($host,$username,$password);  
    if (!$link) {  
        die(mysql_error());  
    }
    $db_selected = mysql_select_db($database);  
    if (!$db_selected) {  
        die(mysql_error());  
    }  
    $result = mysql_query($query);  
    mysql_close($link);  
    return $result;  
}  

function add_img($name) {  
    $add_to_db = $this->query("INSERT INTO files
            (type, item_id, image)
            VALUES
            ('0', '12', '".$name."')") or die(mysql_error('new_error'));  
    return $add_to_db;  
}
almcd
  • 1,069
  • 2
  • 16
  • 29
Adam Ibrahim
  • 95
  • 1
  • 10
  • You're (most likely) receiving a HTML back from the server, while jQuery is expecting JSON. Check server response in your browsers dev tools. – Artur Filipiak Oct 02 '16 at 09:27
  • at the network when i click upload i'm getting type = xhr – Adam Ibrahim Oct 02 '16 at 09:37
  • 1
    You're looking at the upload request. You should check server sesponse tab (the data that is comming back from the server when upload is done). The proper JSON response should looks like this: `{"files":[{...}]}`. If it looks like e.g: `
    ...
    ` (a HTML or XML), then it's not JSON and for that reason you get the error `Unexpected token < in JSON at position 0`
    – Artur Filipiak Oct 02 '16 at 09:59
  • i'm using chrome at the developer tools there is a tap called network, i can't find tap called server response – Adam Ibrahim Oct 02 '16 at 10:36
  • 1
    Like on this image: https://s16.postimg.org/f345hketx/json.jpg – Artur Filipiak Oct 02 '16 at 10:44
  • ok thank you i'm getting this
    ( ! ) Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in D:\wamp\www\cmslaravel2\public\backend\assets\server\items\Upload
    – Adam Ibrahim Oct 02 '16 at 10:57
  • 1
    So, you have the answer where the problem is... http://stackoverflow.com/questions/21797118/deprecated-mysql-connect – Artur Filipiak Oct 02 '16 at 10:59
  • disable errors solve the issue thank you very much – Adam Ibrahim Oct 02 '16 at 11:08
  • It only masking the problem. Consider switching to `mysqli_` or PDO, as your app may stop working in the future. Glad I could help. – Artur Filipiak Oct 02 '16 at 11:11

0 Answers0