AJAX, JSON, and PHP - Uncaught SyntaxError: Unexpected token < -
so using ajax upload image image manager tool building... of sudden not working... didn't change code or anything.
the .php runs , uploads image, events want fire after json encoded , sent arent happening. :/
the console logs uncaught syntaxerror: unexpected token <
the same things happening on ajax request, fine on third one...
in past when error, php syntax error, have not updated .php in while.. soooo im stumped.
here javascript:
$("#chosenfile").change(function(){ if($(this).attr('name')) { var data; data = new formdata(); data.append('uploadedfile', $( '#chosenfile' )[0].files[0]); $('#loader').css('display','block'); $.ajax({ url: '/includes/uploadimage.php', data: data, processdata: false, contenttype: false, type: 'post', success: function ( data ) { //console.log(data); //returns string of data. var image = json.parse(data); //parses string object. console.log(image); //logs object. if (image.error) { alert(image.error); $('#remove').click(); $('#loader').css('display','none'); } else { if (image.link) { //if image uploaded , returned link. $('#remove').click(); $('#loader').css('display','none'); $('#scrollwindow').append("<div class='you'><img imgid='" + image.id + "' src='" + image.link + "' alt=''><span class='delete'>x</span></div>").fadein(200); addtoslider(1); }; } } }); } });
here php:
<?php include '../includes/global.php'; // general vars , functions use sitewide. /* ------------------------- */ /* -- lets file! -- */ /* ------------------------- */ $file = $_files["uploadedfile"]; $return = array(); $status = "failure"; /* ------------------------- */ /* - okay, lets upload it! - */ /* ------------------------- */ $return["type"] = $file["type"]; $target_path = "../uploads/"; // define folder upload $allowedexts = array("jpeg", "jpg", "jpg", "jpeg", "image/jpg"); // array of allowed extensions. $extension = end(explode("/", $file["type"])); // find extension. $maxsize = 2097152; // max file size hidden field in form. if ($file) { if (in_array($extension, $allowedexts)) // if extension allowed. { if(($file["size"] < $maxsize)) { // if size less max file size. $uploadresponse = "the file size - "; $target_path = $target_path . basename($file['name']); //add file name string upload folder destination string. $imagelink = "http://scoutsamerica.com/uploads/" . basename( $file['name']); // full link if (file_exists($target_path)) { // if file exists, add random integer end of after "_". $uploadresponse .= "this file exists: " . $target_path; $randomnum = rand(1, 9999999); $crudparts = explode(".", $file["name"]); //split filename , extension again. $exceptext = $crudparts[0]; //filename $extension = $crudparts[1]; //extension $target_path = "../uploads/" . $exceptext . "_" . $randomnum . "." . $extension; //rename random number. $imagelink = "http://scoutsamerica.com/uploads/" . $exceptext . "_" . $randomnum . "." . $extension; $uploadresponse .= " path changed to: " . $target_path; } if(move_uploaded_file($file['tmp_name'], $target_path)) { $uploadresponse .= "the file ". basename( $file['name']) . " has been uploaded " . $target_path . "</br></br>"; } else { $uploadresponse .= "there error uploading file, please try again! </br>"; $uploadresponse .= "file size: " . ($file["size"] / 1024) . "kb</br>"; $uploadresponse .= "max size: " . ($maxsize / 1024) . "kb"; } $status = "success"; $return["link"] = $imagelink; /* ---------------------------------- */ /* - time upload image, yo. - */ /* ---------------------------------- */ $imagesql = "insert images (id, link, model_id, addedon) values (". "null, " . prepsql($imagelink) . ", " . prepsql($myid) . ", " . prepsql($date) . ")"; mysql_query($imagesql) or die("images: " . mysql_error()); } else { $uploadresponse = "the file must less " . ($maxsize / 1024) . "kb."; $status = "failure"; $return["error"] = $uploadresponse; } } else { $uploadresponse = "the file must .jpg file."; $status = "failure"; $return["error"] = $uploadresponse; } } else { $uploadresponse = "no image."; $status = "failure"; $return["error"] = $uploadresponse; } /* ------------------------- */ /* - lets send :) - */ /* ------------------------- */ $return["status"] = $status; $return["id"] = mysql_insert_id(); $return["traveler"] = $file; str_replace('\\/', '/', json_encode($return)); echo json_encode($return); ?>
raw response:
<script> function haireyesfind(haireyes){ if (haireyes == "blonde") { return "z"; }; if (haireyes == "dirty blonde") { return "b"; }; if (haireyes == "auburn") { return "c"; }; if (haireyes == "brown") { return "d"; }; if (haireyes == "black") { return "e"; }; if (haireyes == "red") { return "f"; }; if (haireyes == "blue2") { return "g"; }; if (haireyes == "green2") { return "h"; }; if (haireyes == "hazel2") { return "i"; }; if (haireyes == "brown2") { return "j"; }; } </script>{"type":"image\/jpeg","link":"http:\/\/scoutsamerica.com\/uploads\/485604_10201093620571706_1239548317_n_5119195.jpg","status":"success","id":281,"traveler":{"name":"485604_10201093620571706_1239548317_n.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpx1qywo","error":0,"size":60368}}
unexpected token
means you've got corrupted json response server. it's html-formatted php error/warning, embedded before/after json data, e.g.
<p>php warning: blah blahblah</p>[...json text here ...]
since html not valid json, error message. check out raw response server particular ajax call, , see what's coming server.
Comments
Post a Comment