第一次尝试使用JSON。 这里是我的checklink.php:PHP-JSON:检查损坏的链接
function url_exists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
if ($retcode == 400){
return "false";
}else{
return "true";
}
curl_close($ch);
}
$response = array(
'location' => $location,
'status' => $status
);
$rr = url_exists($response['location']);
echo json_encode($rr);
JS部分:
function UrlExistsNew(url, callback) {
$.getJSON('checklink.php', { location: url }, function (data) {
callback.apply(null, data.status);
});
}
...
UrlExistsNew($(this).val(), function(status){
if(status === "false") $(element).css('background-color','#FC0');
});
...
看来PHP页面没有返回结果为JSON查询。
编辑:请注意,我忘了安装卷曲在我的服务器启用它。我希望没有人会错过这个。
不必返回任何结果都只是真的还是假的? json在哪里? – thecodeparadox
@thecodeparadox我不明白你想问什么。我不知道如何在PHP和JSON方面做什么。你的意思是json在哪里? – xperator
尝试一些debbugging工具,如firebug或chrome开发工具.. – Vytautas