为什么我不能通过使用curl_setopt和Ajax jquery获取数据?

Hen*_*eak 5 javascript php ajax jquery json

我是通过使用curl_setopt PHP和Ajax查询来自另一个服务器的数据 我已经测试了我的下面的功能它是工作但它似乎不够顺利,因为有时当我尝试刷新和互联网有点大慢我永远不会得到数据.当我在Firefox中检查firebug时,我发现如下json响应.

"<html><head>

<meta http-equiv="refresh" content="0;url=http:MyPartnerWebsite!queryLuckNumberRecordByPeriods.action?gwqqnhmpepceiqqn">

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="expires" content="-1">

</head><body></body></html>

"
Run Code Online (Sandbox Code Playgroud)

*我现在被卡住了,因为我不知道如何应对这个问题*

private function http_code() {

    $ch = curl_init();
    if (!$ch) {
        die("Couldn't initialize a cURL handle");
    }

    $ret = curl_setopt($ch, CURLOPT_URL, "http://Mypartnerwebsite!queryLuckNumberRecordByPeriods.action");
    $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_setopt($ch, CURLINFO_PRETRANSFER_TIME, 0.1);
    $ret = curl_setopt($ch, CURLINFO_HTTP_CODE, true);
    $ret = curl_setopt($ch, CURLOPT_PRIVATE, false);
    $ret = curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $ret = curl_exec($ch);
    $data;
    $res;
    if (empty($ret)) {
        $res = 0;
        $data = 0; // Partner server slow.
        curl_close($ch);
    } else {

        $info = curl_getinfo($ch);
        curl_close($ch);
        if (empty($info['http_code'])) {
            $res = 01;
            $data = 01; // Got empty date from partner server
        } else {
            if ($this->errorType($info['http_code']) == TRUE) { // errors type
                $data = $ret;
                $res = $this->errorType($info['http_code']);
            } else {
                $data = $ret;
                $res = 'unknowError';
            }
        }
    }
    echo json_encode(array('res' => $res, 'data' => $ret));
}
Run Code Online (Sandbox Code Playgroud)

这是一个检查知道什么类型的服务器错误响应的功能 目的我想检查我的伙伴服务器错误,我将这种错误发送到我的网站,以确保出现什么样的错误.

private function errorType($haystack) {

    $errors = array(404, 200, 201, 204, 500, 501, 502, 503, 504, 505, 100, 203);
    if (in_array($haystack, $errors)) {
        return $haystack;
    } else {
        return FALSE;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是Jquery Ajax从我自己的服务器获取数据

$.ajax({
    method: "GET",
    url: "<?PHP echo base_url('getcurrentday'); ?>",
    dataType: "JSON",
    beforeSend: function (xhr) {
    },
    success: function (data, textStatus, jqXHR) {
        $(".loading").html("");
        if (data.res !== 200) {
            $("<p>Errors type:"+data.res+"</p>").appendTo(".errors");
        } else {
        if (data.data == false) {
            getdata();// I want to reload Ajax if I got respond false and my data = '';
        }
        $.each(eval(ldata), function (i, val) {
            $(val.anydata").appendTo(".result");
        })
    }
    }
});
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Bas*_*ein 1

您的呼叫似乎ajax超时,因此并不总是有效。可以增加ajax的超时设置。

此外,您可以添加一个error函数来查看何时发生超时等错误。

$.ajax({
    //your ajax code here...
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    },
    timeout: 10000 // sets timeout to 10 seconds
});
Run Code Online (Sandbox Code Playgroud)

您仍然在 firebug 中看到响应的原因是浏览器无论如何都会收到服务器响应,即使超时,但它不会到达您的 jquery 代码。