I try to parse a string to a jquery object, but I dont get it in the right format. Don't know where the mistake is.
My goal is to load the data from database an put it into a dialog, where I can update the user
I have a post request in my js file which is executed pressing a button:
$.post("index.php", { "id": ID , "action": "edit_row"}, function(input){ console.log(input); //{"id":"1","fname":"Test","lname":"Name"} console.log(input.id); //undefined var data = $.parseJSON(input); //Uncaught SyntaxError: Unexpected token console.log(data); //not possible console.log(data.id); //not possible test = {"id":"1","fname":"Test","lname":"Name"}; console.log(test); // I get the object console.log(test.id); //1 }); // when I use ',"json"' between the brackets nothing happens, no logs are written in console my index.php throws the string back loaded from the database:
$query = "SELECT `id`, `fname` , `lname` FROM `user` WHERE `id`= ".$id; //it's just one row $result = sql_query($query); while ($row= mysql_fetch_assoc($result)){ $data = $row; }; echo json_encode($data); //php function function sql_query($query){ $result = mysql_query($query); if(!$result) { die("Request Failed: " . mysql_error()); } return $result; } Edit 2
I analysed the string response in the js file And I found out that the string gehts \r\n\r\n\r\n to the end. Even when I don't respond anything from the php file...
I think there is something corrupt in my index.php while handling the ajax request.
I did the request on a different php file and there it works without any problems.
$.post("test.php", { "id": ID , "action": "edit_row"}, function(input){ console.log(input); // I get the object console.log(input.id); //1 //testdata below test = {"id":"1","fname":"Test","lname":"Name"}; console.log(test); // I get the object console.log(test.id); //1 },"json"); //using ',"json"' is working on the test.php file now Now it would be nice to know why I get those new lines even when I dont respond anything
But I think that doesnt fit to the head querstion