Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode adds backslashes

I am getting some data from mysql and sending it to sever using json. But sending data as an array adds backslashes to my results.

PHP:

$result = mysql_query("SELECT location_name, phone_number FROM location WHERE email_id = '[email protected]'");

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row;
}
 echo json_encode($result_array);

JS:

    $$.ajax({
            url: 'http://www.abc.co/SupportData/get_business_locations.php', 
            type: "POST",
            data: {
                email: window.localStorage["email"]               
            },
            dataType: "JSON",
            success: function (jsonStr) {
               console.log(JSON.stringify(jsonStr));
            }
        });

Output :

[{\"location_name\":\"Bandra\",\"phone_number\":\"\"},{\"location_name\":\"Dadar\",\"phone_number\":\"\"},{\"location_name\":\"ee\",\"phone_number\":\"\"},{\"location_name\":\"ttttt\",\"phone_number\":\"\"}]""

like image 994
Nehil Mistry Avatar asked Oct 18 '25 23:10

Nehil Mistry


1 Answers

stripslashes(json_encode($result_array))

worked for me!

like image 140
Witte Vador Avatar answered Oct 20 '25 14:10

Witte Vador