gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:58 24 4
gpt4 key购买 nike

我正在使用 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>

"

*我现在卡住了,因为我不知道如何处理这个响应 *

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));
}

这是一个检查服务器错误响应类型的函数目的 我想检查我的合作伙伴服务器何时出错,我会将此类错误发送到我的网站以确定是哪种错误。

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;
}
}

这里是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");
})
}
}
});

请帮忙。

最佳答案

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

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

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

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

关于javascript - 为什么我不能使用 curl_setopt 和 Ajax jquery 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34218473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com