Javascript 中止jquery ajax时服务器响应延迟
当我们中止ajax请求时,我从PHP服务器得到了很晚的ajax响应。 我正在为服务器脚本使用codeigniter框架 Javascript代码:Javascript 中止jquery ajax时服务器响应延迟,javascript,php,ajax,codeigniter,Javascript,Php,Ajax,Codeigniter,当我们中止ajax请求时,我从PHP服务器得到了很晚的ajax响应。 我正在为服务器脚本使用codeigniter框架 Javascript代码: cblcurrentRequest = $.ajax({ url: baseurl + 'Login/getChannelBrand/' + brand_id, type: 'post', async: true, cache: true,
cblcurrentRequest = $.ajax({
url: baseurl + 'Login/getChannelBrand/' + brand_id,
type: 'post',
async: true,
cache: true,
data: '',
beforeSend: function () {
$('#tw').html("");
$('#fb').html("");
$('#inst').html("");
$('#yt').html("");
if (cblcurrentRequest != null) {
cblcurrentRequest.abort();
}
if (firsttwcurrentRequest != null) {
firsttwcurrentRequest.abort();
}
if (firstfbcurrentRequest != null) {
firstfbcurrentRequest.abort();
}
if (firstigcurrentRequest != null) {
firstigcurrentRequest.abort();
}
if (firstytcurrentRequest != null) {
firstytcurrentRequest.abort();
}
if (twcurrentRequest != null) {
twcurrentRequest.abort();
}
if (fbcurrentRequest != null) {
fbcurrentRequest.abort();
}
if (igcurrentRequest != null) {
igcurrentRequest.abort();
}
if (ytcurrentRequest != null) {
ytcurrentRequest.abort();
}
},
success: function (result) {
var channelBrandList = JSON.parse(result);
facebookBrandName = channelBrandList['FacebookChannel'];
twitterBrandName = channelBrandList['TwitterChannel'];
youtubeBrandName = channelBrandList['YoutubeChannel'];
instagramBrandName = channelBrandList['InstagramChannel'];
YelpBrandName = channelBrandList['YelpChannel'];
console.log(facebookBrandName);
console.log(twitterBrandName);
console.log(youtubeBrandName);
console.log(instagramBrandName);
console.log(yelpBrandName);
}
})
function getChannelBrandList($brandID) {
$query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
if (count($query->result()) > 0) {
return $query->result();
} else {
return $query = "";
}
}
{
"BrandId":"26",
"BrandName":"jeep",
"FacebookChannel":"jeep",
"TwitterChannel":"jeep",
"YoutubeChannel":"thejeepchannel",
"InstagramChannel":"jeep",
"YelpChannel":null
}
登录控制器
function getChannelBrand($brandID) {
$channelBrandList = $this->Model_brand->getChannelBrandList($brandID);
$channelbrand_array = array('BrandId' => $channelBrandList[0]->BrandId,
'BrandName' => $channelBrandList[0]->BrandName,
'FacebookChannel' => $channelBrandList[0]->FacebookChannel,
'TwitterChannel' => $channelBrandList[0]->TwitterChannel,
'YoutubeChannel' => $channelBrandList[0]->YoutubeChannel,
'InstagramChannel' => $channelBrandList[0]->InstagramChannel,
'YelpChannel' => $channelBrandList[0]->YelpChannel
);
echo(json_encode($channelbrand_array));
}
function getChannelBrand($brandID) {
$channelBrandList = $this->Model_brand->getChannelBrandList($brandID);
if($channelBrandList){
$channelbrand_array = array('BrandId' => $channelBrandList->BrandId,
'BrandName' => $channelBrandList->BrandName,
'FacebookChannel' => $channelBrandList->FacebookChannel,
'TwitterChannel' => $channelBrandList->TwitterChannel,
'YoutubeChannel' => $channelBrandList->YoutubeChannel,
'InstagramChannel' => $channelBrandList->InstagramChannel,
'YelpChannel' => $channelBrandList->YelpChannel
);
echo(json_encode($channelbrand_array));
}
else{
echo "error";
}
}
型号代码:
cblcurrentRequest = $.ajax({
url: baseurl + 'Login/getChannelBrand/' + brand_id,
type: 'post',
async: true,
cache: true,
data: '',
beforeSend: function () {
$('#tw').html("");
$('#fb').html("");
$('#inst').html("");
$('#yt').html("");
if (cblcurrentRequest != null) {
cblcurrentRequest.abort();
}
if (firsttwcurrentRequest != null) {
firsttwcurrentRequest.abort();
}
if (firstfbcurrentRequest != null) {
firstfbcurrentRequest.abort();
}
if (firstigcurrentRequest != null) {
firstigcurrentRequest.abort();
}
if (firstytcurrentRequest != null) {
firstytcurrentRequest.abort();
}
if (twcurrentRequest != null) {
twcurrentRequest.abort();
}
if (fbcurrentRequest != null) {
fbcurrentRequest.abort();
}
if (igcurrentRequest != null) {
igcurrentRequest.abort();
}
if (ytcurrentRequest != null) {
ytcurrentRequest.abort();
}
},
success: function (result) {
var channelBrandList = JSON.parse(result);
facebookBrandName = channelBrandList['FacebookChannel'];
twitterBrandName = channelBrandList['TwitterChannel'];
youtubeBrandName = channelBrandList['YoutubeChannel'];
instagramBrandName = channelBrandList['InstagramChannel'];
YelpBrandName = channelBrandList['YelpChannel'];
console.log(facebookBrandName);
console.log(twitterBrandName);
console.log(youtubeBrandName);
console.log(instagramBrandName);
console.log(yelpBrandName);
}
})
function getChannelBrandList($brandID) {
$query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
if (count($query->result()) > 0) {
return $query->result();
} else {
return $query = "";
}
}
{
"BrandId":"26",
"BrandName":"jeep",
"FacebookChannel":"jeep",
"TwitterChannel":"jeep",
"YoutubeChannel":"thejeepchannel",
"InstagramChannel":"jeep",
"YelpChannel":null
}
Json响应:
cblcurrentRequest = $.ajax({
url: baseurl + 'Login/getChannelBrand/' + brand_id,
type: 'post',
async: true,
cache: true,
data: '',
beforeSend: function () {
$('#tw').html("");
$('#fb').html("");
$('#inst').html("");
$('#yt').html("");
if (cblcurrentRequest != null) {
cblcurrentRequest.abort();
}
if (firsttwcurrentRequest != null) {
firsttwcurrentRequest.abort();
}
if (firstfbcurrentRequest != null) {
firstfbcurrentRequest.abort();
}
if (firstigcurrentRequest != null) {
firstigcurrentRequest.abort();
}
if (firstytcurrentRequest != null) {
firstytcurrentRequest.abort();
}
if (twcurrentRequest != null) {
twcurrentRequest.abort();
}
if (fbcurrentRequest != null) {
fbcurrentRequest.abort();
}
if (igcurrentRequest != null) {
igcurrentRequest.abort();
}
if (ytcurrentRequest != null) {
ytcurrentRequest.abort();
}
},
success: function (result) {
var channelBrandList = JSON.parse(result);
facebookBrandName = channelBrandList['FacebookChannel'];
twitterBrandName = channelBrandList['TwitterChannel'];
youtubeBrandName = channelBrandList['YoutubeChannel'];
instagramBrandName = channelBrandList['InstagramChannel'];
YelpBrandName = channelBrandList['YelpChannel'];
console.log(facebookBrandName);
console.log(twitterBrandName);
console.log(youtubeBrandName);
console.log(instagramBrandName);
console.log(yelpBrandName);
}
})
function getChannelBrandList($brandID) {
$query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
if (count($query->result()) > 0) {
return $query->result();
} else {
return $query = "";
}
}
{
"BrandId":"26",
"BrandName":"jeep",
"FacebookChannel":"jeep",
"TwitterChannel":"jeep",
"YoutubeChannel":"thejeepchannel",
"InstagramChannel":"jeep",
"YelpChannel":null
}
控制台日志:
cblcurrentRequest = $.ajax({
url: baseurl + 'Login/getChannelBrand/' + brand_id,
type: 'post',
async: true,
cache: true,
data: '',
beforeSend: function () {
$('#tw').html("");
$('#fb').html("");
$('#inst').html("");
$('#yt').html("");
if (cblcurrentRequest != null) {
cblcurrentRequest.abort();
}
if (firsttwcurrentRequest != null) {
firsttwcurrentRequest.abort();
}
if (firstfbcurrentRequest != null) {
firstfbcurrentRequest.abort();
}
if (firstigcurrentRequest != null) {
firstigcurrentRequest.abort();
}
if (firstytcurrentRequest != null) {
firstytcurrentRequest.abort();
}
if (twcurrentRequest != null) {
twcurrentRequest.abort();
}
if (fbcurrentRequest != null) {
fbcurrentRequest.abort();
}
if (igcurrentRequest != null) {
igcurrentRequest.abort();
}
if (ytcurrentRequest != null) {
ytcurrentRequest.abort();
}
},
success: function (result) {
var channelBrandList = JSON.parse(result);
facebookBrandName = channelBrandList['FacebookChannel'];
twitterBrandName = channelBrandList['TwitterChannel'];
youtubeBrandName = channelBrandList['YoutubeChannel'];
instagramBrandName = channelBrandList['InstagramChannel'];
YelpBrandName = channelBrandList['YelpChannel'];
console.log(facebookBrandName);
console.log(twitterBrandName);
console.log(youtubeBrandName);
console.log(instagramBrandName);
console.log(yelpBrandName);
}
})
function getChannelBrandList($brandID) {
$query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
if (count($query->result()) > 0) {
return $query->result();
} else {
return $query = "";
}
}
{
"BrandId":"26",
"BrandName":"jeep",
"FacebookChannel":"jeep",
"TwitterChannel":"jeep",
"YoutubeChannel":"thejeepchannel",
"InstagramChannel":"jeep",
"YelpChannel":null
}
如果需要框架级或其他任何配置,请提出建议
谢谢,
Sameek如果您使用下面的查询,这将大大加快查询速度 仅搜索一行
并使用num_rows()
计算行数并返回false
型号
<?php
function getChannelBrandList($brandID) {
$query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
if ($query->num_rows() > 0) {
return $query->row();
} else {
return FALSE;
}
}
只有当Ajax中止时,它才会慢下来,在其他情况下它没有任何问题。考虑两件事会加快查询速度1。使用类似于selectcol1,col2
notselect*
,2的查询。对返回的结果而不是对象使用数组。数组比对象快。问题:您能显示请求的时间吗?如果没有ajax中止,则需要-1076ms。使用ajax中止需要:22925毫秒,还包括显示时间范围的屏幕截图。您的pconnect
设置是什么?在database.php'pconnect'=>FALSE中,尝试pconnect'=>TRUE