SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Solution 1
The fact the character is a < make me think you have a PHP error, have you tried echoing all errors.
Since I don't have your database, I'm going through your code trying to find errors, so far, I've updated your JS file
$("#register-form").submit(function (event) {
var entrance = $(this).find('input[name="IsValid"]').val();
var password = $(this).find('input[name="objPassword"]').val();
var namesurname = $(this).find('input[name="objNameSurname"]').val();
var email = $(this).find('input[name="objEmail"]').val();
var gsm = $(this).find('input[name="objGsm"]').val();
var adres = $(this).find('input[name="objAddress"]').val();
var termsOk = $(this).find('input[name="objAcceptTerms"]').val();
var formURL = $(this).attr("action");
if (request) {
request.abort(); // cancel if any process on pending
}
var postData = {
"objAskGrant": entrance,
"objPass": password,
"objNameSurname": namesurname,
"objEmail": email,
"objGsm": parseInt(gsm),
"objAdres": adres,
"objTerms": termsOk
};
$.post(formURL,postData,function(data,status){
console.log("Data: " + data + "\nStatus: " + status);
});
event.preventDefault();
});
PHP Edit:
if (isset($_POST)) {
$fValid = clear($_POST['objAskGrant']);
$fTerms = clear($_POST['objTerms']);
if ($fValid) {
$fPass = clear($_POST['objPass']);
$fNameSurname = clear($_POST['objNameSurname']);
$fMail = clear($_POST['objEmail']);
$fGsm = clear(int($_POST['objGsm']));
$fAddress = clear($_POST['objAdres']);
$UserIpAddress = "hidden";
$UserCityLocation = "hidden";
$UserCountry = "hidden";
$DateTime = new DateTime();
$result = $date->format('d-m-Y-H:i:s');
$krr = explode('-', $result);
$resultDateTime = implode("", $krr);
$data = array('error' => 'Yükleme Sırasında Hata Oluştu');
$kayit = "INSERT INTO tbl_Records(UserNameSurname, UserMail, UserGsm, UserAddress, DateAdded, UserIp, UserCityLocation, UserCountry, IsChecked, GivenPasscode) VALUES ('$fNameSurname', '$fMail', '$fGsm', '$fAddress', '$resultDateTime', '$UserIpAddress', '$UserCityLocation', '$UserCountry', '$fTerms', '$fPass')";
$retval = mysql_query( $kayit, $conn ); // Update with you connection details
if ($retval) {
$data = array('success' => 'Register Completed', 'postData' => $_POST);
}
} // valid ends
}echo json_encode($data);
Solution 2
For the benefit of searchers looking to solve a similar problem, you can get a similar error if your input is an empty string.
e.g.
var d = "";
var json = JSON.parse(d);
or if you are using AngularJS
var d = "";
var json = angular.fromJson(d);
In chrome it resulted in 'Uncaught SyntaxError: Unexpected end of input', but Firebug showed it as 'JSON.parse: unexpected end of data at line 1 column 1 of the JSON data'.
Sure most people won't be caught out by this, but I hadn't protected the method and it resulted in this error.
Solution 3
Remove
dataType: 'json'
replacing it with
dataType: 'text'
Solution 4
I have the exact same issue and I've found something. I've commented the line :
dataType : 'json',
after that it was successful but... when I did console.log(data) it returned the main index.html.
That's why you have "Unexpected token <" error and it cannot parse.
Solution 5
Changing the data type to text helped dataType: 'text'
I have check with JSONlint and my json format was proper. Still it was throwing error when I set dataType: 'json'
JSON Data: {"eventinvite":1,"groupcount":8,"totalMessagesUnread":0,"unreadmessages":{"378":0,"379":0,"380":0,"385":0,"390":0,"393":0,"413":0,"418":0}}
levent kaya
Updated on July 05, 2022Comments
-
levent kaya almost 2 years
I've spend over 6 hours to find an exception or a special character to find in my code but I couldn't. I checked every similar messages in here.
I'm sending the form with magnific popup. First I'm using inline popup to open my form than I'm sending all inputs to main.js to validate.
So, I just need a third-eye.
I've got:
index.html
,register.php
,main.js
Here's the code
Here goes the error messages
JSON Output
Chrome Console:
Firefox console :
What am i missing?
-
levent kaya over 9 yearslet me check. also here is my database : link
-
levent kaya over 9 yearswhere did you change on here? its like the same as old.. also i've tried doesn't work.
-
James Lalor over 9 yearsIt was html markup being on a second line - this can cause problems if it's not on the same line as the opening " or '
-
levent kaya over 9 yearsi tried. u chagenged my 'gsm' from varchar to int and i changed it to intval() but nothing happens. something still missing. it gives : TypeError: data is null
-
levent kaya over 9 years'console.log(data.success)' (JS file) It says : 'Uncaught TypeError: Cannot read property 'success' of null'
-
James Lalor over 9 yearsconsole.log(data) and see what it says
-
levent kaya over 9 yearsjust 'null' :) trying to find in php page.
-
James Lalor over 9 yearsI've just updated the JS a bit, can you tell me what it returns?
-
levent kaya over 9 yearshere results : 'Data: {"success":"Register Completed","postData":{"objAskGrant":"Yes","objPass":"12312312","objNameSurname":"alskdjfl;asdkjf","objEmail":"asdfasdf@asdfasdf.com","objGsm":"3412341234","objAdres":"asdfasdf asdfasdfasf","objTerms":"CheckIsValid"}} Status: success'
-
levent kaya over 9 yearsofftopic : link this song is dedicated to you! thx man
-
Mujtaba over 4 yearsHey, It was really helpful, i were stuffing my mind for last 48 hours and it helped me, can you please explain why its behaving like that? Thank you. and +1 for the hint.
-
ParisaN over 2 yearsYes, my url was wrong. thankyou.
-
Wizard about 2 yearsthis was the case for me. If the path is wrong the response will not be valid for JSON formatting.