1

I try add data to database using ajax. I prepare data:

$(".panel").on('click','.buttons_save',function() {
    groupIdD = this.id;
    groupNameD=$("#groupName").val();
    groupDescriptionD=$("#groupDescription").val();
    paramGroupD=$("#paramGroup").val();
    paramOrderD=$("#paramOrder").val();

    $.ajax({
        dataType: "json",
        type: "POST",
        url: ajaxSetGroupParams,
        data: { 
            groupId: groupIdD,
            paramGroup: groupNameD,
            groupName: groupDescriptionD,
            groupDescription: paramGroupD,
            paramOrder: paramOrderD
        }
    })

    .done(function() {
        alert( "success" );
    })
    .fail(function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    })


});

And I add this data to database (everything is ok, data are add to database). $polaczenie_z_baza=$_SESSION['db']['connectObjs']['system'];

$id_group=$_REQUEST['groupId'];
$paramGroup=$_REQUEST['paramGroup'];
$groupName=$_REQUEST['groupName'];
$groupDescription=$_REQUEST['groupDescription'];
if (isset($_REQUEST['paramOrder'])) {
    $paramOrder=$_REQUEST['paramOrder'];
}
else {
    $paramOrder=0;
}

$qUpdateData    = '
UPDATE
`grupy_parametrow_konfiguracyjnych`
SET
`id_grupa_nadrzedna` ='.$paramGroup.',
`nazwa_grupy` = '.$groupName.',
`opis_grupy` = '.$groupDescription.',
`kolejnosc` ='.$paramOrder.'
WHERE
`id_grupa_parametrow` = '.$id_group; 

$result = stdIUDQuery( $qUpdateData, $polaczenie_z_baza);

But always I have fail information:

parsererror
SyntaxError: JSON.parse: unexpected end of data

return window.JSON.parse( data );
3
  • Show your json data structure Commented Apr 2, 2014 at 8:26
  • I hope this is only a minimal example, if not you should take care for SQL Injection techniques. Commented Apr 2, 2014 at 8:35
  • This is modified example. I have data validation and prepared statments. Commented Apr 2, 2014 at 8:39

2 Answers 2

3

This will be happening because your server isn't returning JSON, but you're telling jQuery that it is. Since you're not using the contents of the response from the server in your done handler, you can simply remove the dataType: "json", from your $.ajax call.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Now everything is ok.
0

3 ideas also to debug the error.

A firefox

  1. save the response in notepad as idea.json
  2. open in firefox
  3. it will show you if is good json or not.

B console.log(variable.constructor)

check if is string or json with console.log(variable_name.constructor);

C check the correct line

check all JSON.parse lines, my problem was for 20 mins checking the wrong line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.