PHP: Whois via AJAX



我非常不擅长AJAX(实际上,我刚刚开始学习它)。

所以,我在PHP上写whois服务,我想通过ajax请求输出结果。

我现在只有:

我的PHP代码:
$domain = $_POST['domain'];
$whois = new Whois();
header("content-type:application/json");
$res = $whois->getWhois($domain); // Calls the Whois-query function;
echo json_encode($res);
我的JS代码:
$('#submit').on('click', function() {
    e.preventDefault();
    var domain = $('#value').val();
});
$.ajax({
    url: 'ajax/whois.php',
    type: "post",
    data: {'domain': domain, 'action': 'whois'},
    dataType: "json",
    success: function(json) {
        $('#whoisResult').html('<h2>Whois Query result for ' + domain + '</h2>');
        $('#whoisContent').html(json.html);
    },
    error: function(xhr, status) {
        $('#whoisResult').html('<h2>Sorry, an error occured. Try again later, please!</h2>')
    }
});

作为HTML,我有一个输入:<input type="text" id="value">和提交按钮。

我搜索脚本的例子,并试图使类似的东西,但它不工作在所有…

注:我猜你不会给这个问题一个负面评价:)

最大功率S:按要求,这是我从PHP的响应:

{"domain":"exp.cm","whois":"[Domain]nDomain: exp.cmnStatus: activenChanged: 2014-02-25T12:22:00.957819+02:00nn[Holder]nType: Legal personnName: Name, SurnamenEmail: email@example.comnPhone: Phone herenAddress: Address goes herenSome other infonnUpdated: 2014-03-18T18:12:35.717462+00:00n"}

在这种情况下,您不需要JSON数据类型,只需返回html。

此外,您将希望ajax请求位于单击事件内部。在单击事件中,您还忘记传递e参数。

$domain = $_POST['domain'];
$whois = new Whois();
header("content-type:text/html");
$res = $whois->getWhois($domain); // Calls the Whois-query function;
echo $res;

js:

$('#submit').on('click', function(e) {
    e.preventDefault();
    var domain = $('#value').val();
    $.ajax({
        url: 'ajax/whois.php',
        type: "post",
        data: {'domain': domain, 'action': 'whois'},
        //dataType: "json",
        success: function(html) {
            $('#whoisResult').html('<h2>Whois Query result for ' + domain + '</h2>');
            $('#whoisContent').html(html);
        },
        error: function(xhr, status) {
            $('#whoisResult').html('<h2>Sorry, an error occured. Try again later, please!</h2>')
        }
    });
});

最新更新