0

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

Yddap17
  • 15
  • 3
  • Tip: Don't try and generate the JSON by hand. Use a built-in language function. – ʰᵈˑ Sep 05 '14 at 13:57
  • 1
    @ʰᵈˑ kind of useless tip since he is using the json_encode in it's php. the other stuff is just test data he put in – Pinoniq Sep 05 '14 at 13:58
  • Line 1 of the jQuery file - looks like he's building it by hand... – ʰᵈˑ Sep 05 '14 at 13:58
  • possible duplicate of [What is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()](http://stackoverflow.com/questions/18130081/what-is-causing-uncaught-syntaxerror-unexpected-token-o-with-parsejson-an) – Pinoniq Sep 05 '14 at 13:59
  • "the other stuff is just test data he put in"... test data – Luke Sep 05 '14 at 13:59
  • Nothing wrong with that first line. JSON is the built in way in js – Pinoniq Sep 05 '14 at 14:00
  • @Pinoniq Yes, the input or data should work as the generated test - but it doesn't work as is should (I think) – Yddap17 Sep 05 '14 at 14:01

1 Answers1

1

use this statement on top of your index.php page

<?php error_reporting(0); ?>

Since you are using mysql_query it gives a warning message for deprecated function which is causing the error. The whole warning message is returned to your page which is not in the format you expect. This statement will suppress the warning and it should be fine. But i would suggest you to use PDO or mysqli.

subas_poudel
  • 456
  • 2
  • 17