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

473,802

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}} 
Share:
473,802
levent kaya
Author by

levent kaya

Updated on July 05, 2022

Comments

  • levent kaya
    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

    FORM

    JS/AJAX

    PHP-register.php

    Here goes the error messages

    JSON Output json

    Chrome Console:

    chrome

    Firefox console : firefox


    What am i missing?

  • levent kaya
    levent kaya over 9 years
    let me check. also here is my database : link
  • levent kaya
    levent kaya over 9 years
    where did you change on here? its like the same as old.. also i've tried doesn't work.
  • James Lalor
    James Lalor over 9 years
    It 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
    levent kaya over 9 years
    i 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
    levent kaya over 9 years
    'console.log(data.success)' (JS file) It says : 'Uncaught TypeError: Cannot read property 'success' of null'
  • James Lalor
    James Lalor over 9 years
    console.log(data) and see what it says
  • levent kaya
    levent kaya over 9 years
    just 'null' :) trying to find in php page.
  • James Lalor
    James Lalor over 9 years
    I've just updated the JS a bit, can you tell me what it returns?
  • levent kaya
    levent kaya over 9 years
    here results : 'Data: {"success":"Register Completed","postData":{"objAskGrant":"Yes","objPass":"123123‌​12","objNameSurname"‌​:"alskdjfl;asdkjf","‌​objEmail":"asdfasdf@‌​asdfasdf.com","objGs‌​m":"3412341234","obj‌​Adres":"asdfasdf asdfasdfasf","objTerms":"CheckIsValid"}} Status: success'
  • levent kaya
    levent kaya over 9 years
    offtopic : link this song is dedicated to you! thx man
  • Mujtaba
    Mujtaba over 4 years
    Hey, 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
    ParisaN over 2 years
    Yes, my url was wrong. thankyou.
  • Wizard
    Wizard about 2 years
    this was the case for me. If the path is wrong the response will not be valid for JSON formatting.