Php jQuery:ajax返回时出错,因为换行符和某些字符
各位,我有个问题。使用jQuery发出ajax请求。这是一个错误。我试图删除一些包含“换行符、双引号等”的变量。没有错误,一切都很好。如何解决这个问题Php jQuery:ajax返回时出错,因为换行符和某些字符,php,jquery,ajax,Php,Jquery,Ajax,各位,我有个问题。使用jQuery发出ajax请求。这是一个错误。我试图删除一些包含“换行符、双引号等”的变量。没有错误,一切都很好。如何解决这个问题 $.ajax({ url: 'layout/owner/required/processing/get-product-data.php', cache: false, data: {idStr: thisProduct} }); 请求的php文件是: <?php header('Content-type: tex
$.ajax({
url: 'layout/owner/required/processing/get-product-data.php',
cache: false,
data: {idStr: thisProduct}
});
请求的php文件是:
<?php
header('Content-type: text/javascript');
$printData = '
var editData = {
cat: "' . $cat . '",
subcat: "' . $subcat . '",
id: "' . $id . '",
name: "' . $name . '",
description: "' . $description . '",
price: "' . $price . '",
dlong: "' . $long . '",
dwidth: "' . $width . '",
dheight: "' . $height . '",
spec: "' . $spec . '",
fac: "' . $fac . '",
rp: "' . $rp . '",
cm: "' . $cm . '",
color: "' . $color . '"
};
';
echo $printData;
?>
editData.description、editData.spec、editData.fac都包含enter/linebreak字符,uhmm说它们包含html将它们放在PHP关联数组中,然后使用输出该数组。这将确保字符串中的字符得到正确处理 例如,类似这样的事情:
<?php
header('Content-type: text/javascript');
$a = array(
cat => $cat,
subcat => $subcat,
id => $id,
name => $name,
description => $description,
price => $price,
dlong => $long,
dwidth => $width,
dheight => $height,
spec => $spec,
fac => $fac,
rp => $rp,
cm => $cm,
color => $color
);
echo 'var editData = ' . json_encode($a) . ';';
?>
将它们放入PHP关联数组中,然后使用输出该数组。这将确保字符串中的字符得到正确处理 例如,类似这样的事情:
<?php
header('Content-type: text/javascript');
$a = array(
cat => $cat,
subcat => $subcat,
id => $id,
name => $name,
description => $description,
price => $price,
dlong => $long,
dwidth => $width,
dheight => $height,
spec => $spec,
fac => $fac,
rp => $rp,
cm => $cm,
color => $color
);
echo 'var editData = ' . json_encode($a) . ';';
?>
好的……1分钟,我将在几秒钟内尝试此操作,然后再回来。好的……1分钟,我将在几秒钟内尝试此操作,然后再回来