这是我的JavaScript代码:
$.ajax({
url: './checkcolors.php',
type: 'post',
data: {
url: '<?php echo $LINK;?>',
SizeId: SelectedSizeID
},
dataType: 'json',
success: function (data) {
$('.ColorImagesNOColor').fadeIn();
$('#LoadingImage').hide();
$.each(data, function(index, item) {
$('#' + data.colorids).hide();
});
}
});
这是我的checkcolors.php:
<?PHP
$url = 'http://www.sportsdirect.com/dunlop-mens-canvas-low-top-trainers-246046?colcode=24604622';
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);
$DataVariants = $xpath->query('//span[@class="ImgButWrap"]/@data-variants')->item(0)->nodeValue;
$jsonStart = strpos($DataVariants, '[');
$jsonEnd = strrpos($DataVariants, ']');
$collections = json_decode(substr($DataVariants, $jsonStart, $jsonEnd - $jsonStart + 1));
foreach ($collections as $item) {
$ColVarId = $item->ColVarId;
$result = [];
$SizeNames = [];
foreach ($item->SizeVariants as $size) {
$SizeNames[] = $size->SizeName;
}
if (in_array("7", $SizeNames)) {
$result = array('colorids' => $ColVarId);
}
echo json_encode($result);
}
?>
checkcolors.php的echo结果如下所示:
{"colorids":"24604603"}{"colorids":"24604684"}{"colorids":"24604640"}{"colorids":"24604609"}{"colorids":"24604682"}{"colorids":"24604686"}{"colorids":"24604681"}{"colorids":"24604689"}{"colorids":"24604602"}{"colorids":"24604679"}{"colorids":"24604680"}{"colorids":"24604622"}{"colorids":"24604685"}{"colorids":"24604683"}{"colorids":"24604621"}{"colorids":"24604677"}{"colorids":"24604688"}
我认为我的json_encode函数存在问题。我认为回声结果不正确。你可以帮我解决一下吗?
提前致谢!