我从mysql获取一些数据并使用json将其发送到服务器。但是,将数据作为数组发送会为我的结果添加反斜杠。
PHP:
$result = mysql_query("SELECT location_name, phone_number FROM location WHERE email_id = 'abc@gmail.com'");
$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));
}
});
输出:
[{\"location_name\":\"Bandra\",\"phone_number\":\"\"},{\"location_name\":\"Dadar\",\"phone_number\":\"\"},{\"location_name\":\"ee\",\"phone_number\":\"\"},{\"location_name\":\"ttttt\",\"phone_number\":\"\"}]""
答案 0 :(得分:1)
要从json中删除反斜杠,请使用col-xs-3
JSON_UNESCAPED_SLASHES
答案 1 :(得分:0)
您可以使用以下代码替换反斜杠。
str_replace("\ " , '' ,"jsonoutput");
答案 2 :(得分:0)
发生的事情是你的AJAX调用已经带回了JSON。因此,如果你在其上做一个JSON.stringify,你实际上是多次JSON编码。请参阅Why is json_encode adding backslashes?,这可能会有帮助。
答案 3 :(得分:0)
stripslashes(json_encode($result_array))
为我工作!