dongzong1866 2012-09-21 03:58
浏览 22
已采纳

如果网站存在AJAX,则验证

I'm trying to check if a website exists with an ajax call, but I'm not sure I am getting it right. On my page I grab a URL on click

$("#go").click(function() {
    var url = $("#url").val();
    $.ajax({
        type: "POST",
        url: "/ajax.php",
        data: "url="+url,
        success: function(){
          $("#start").remove();
        },      
        error: function(){
        alert("Bad URL");
        }
    });     
});

a=And then check on ajax.php

$url = $_POST['url'];

ini_set("default_socket_timeout","05");
set_time_limit(5);
$f=fopen($url,"r");
$r=fread($f,1000);
fclose($f);
if(strlen($r)>1) {
    return true;
} else {
    return false;
}

It seems I am getting SUCCESS no matter what... What am I missing?

  • 写回答

3条回答 默认 最新

  • dtfpznrbn503027700 2012-09-21 04:43
    关注

    It seems I am getting SUCCESS no matter what... What am I missing?

    This is extremely pretty straightforward.

    Because of this reasons:

    // You have no idea what server respond is.
    // that is you can't parse that respond
    success: function(){
       $("#start").remove();
    }
    

    Which should be

    success: function(respond){
    
       //you don't have to return TRUE in your php
       //you have to echo this one instead
       if ( respond == '1'){
         $("#start").remove();
       } else {
         //handle non-true if you need so
       }
    }
    

    In php replace this:

    if(strlen($r)>1) {
        return true;
    } else {
        return false;
    }
    

    to

    if(strlen($r)>1) {
        print true; //by the way, TRUE is a constant and it equals to == 1 (not ===)
    }
    

    Oh yeah, also don't forget to fix this as well:

    data: "url="+url,

    to data : {"url" : url}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据