Jquery ajax漫长的等待时间 [英] Jquery ajax long waiting time
问题描述
我正在使用jQuery ajax在我的asp.net页面中调用webmethod,这里是代码:
I'm using jQuery ajax to call a webmethod in my asp.net page, here's the code:
$.ajax({
type: 'POST',
url: 'Home.aspx/GetPlantInfo',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
info= JSON.parse(response.d);
$("#PlantName").val(info.Name);
.
.
.
},
failure: function (r) {
alert(r.d);
}
});
webmethod执行简单的linq查询并将结果序列化为JSON字符串,这需要不到半秒。这是webmethod代码:
the webmethod does a simple linq query and serializes the result to a JSON string, which takes less than half a sec. here's the webmethod code:
[WebMethod]
public static string GetPlantInfo()
{
MyDataClassesDataContext dc = new MyDataClassesDataContext();
Plant p = (from x in dc.Plants
where x.Name == "someName"
select x).FirstOrDefault();
string plantJson = JsonConvert.SerializeObject(p);
return plantJson;
}
但是ajax调用需要10-15秒才能完成。我检查了ajax时间,显然大部分时间是等待时间,发送和接收时间可以忽略不计。我没有锁定谷歌搜索这个问题。我问的是这个等待的时间究竟是什么?是IIS服务器的问题吗?我的代码有问题吗?
我尝试了什么:
我已经尝试评论c#代码,因此它返回一个空字符串,但仍然有一个等待时间(虽然这次是较小的时间:5~6秒)。
however the ajax call takes 10-15 seconds to complete. I inspected ajax timing and apparently most of this time is "Waiting" time, with negligible send and receive time. I googled this problem with no lock. What I'm asking is what exactly is this "Waiting" time? Is the problem from IIS server? is something wrong with my code?
What I have tried:
i've tried commenting the c# code so it returns an empty string, but still there is a waiting time (although it's lesser this time: 5~6 sec).
推荐答案
.ajax({
type:' POST',
url :' Home.aspx / GetPlantInfo',
contentType:' application / json; charset = utf-8',
dataType:' json',
成功: function (回复){
info = JSON .parse(response.d);
.ajax({ type: 'POST', url: 'Home.aspx/GetPlantInfo', contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (response) { info= JSON.parse(response.d);
( #PlantName跨度>)VAL(info.Name)。
。
。
。
},
失败: function (r){
alert(r.d);
}
});
("#PlantName").val(info.Name); . . . }, failure: function (r) { alert(r.d); } });
webmethod执行简单的linq查询并将结果序列化为JSON字符串,这需要不到半秒。这是webmethod代码:
the webmethod does a simple linq query and serializes the result to a JSON string, which takes less than half a sec. here's the webmethod code:
[WebMethod]
public static string GetPlantInfo()
{
MyDataClassesDataContext dc = new MyDataClassesDataContext();
Plant p = (from x in dc.Plants
where x.Name == "someName"
select x).FirstOrDefault();
string plantJson = JsonConvert.SerializeObject(p);
return plantJson;
}
但是ajax调用需要10-15秒才能完成。我检查了ajax时间,显然大部分时间是等待时间,发送和接收时间可以忽略不计。我没有锁定谷歌搜索这个问题。我问的是这个等待的时间究竟是什么?是IIS服务器的问题吗?我的代码有问题吗?
我尝试了什么:
我已经尝试评论c#代码,所以它返回一个空字符串,但仍然有一个等待时间(虽然这次是较小的时间:5~6秒)。
however the ajax call takes 10-15 seconds to complete. I inspected ajax timing and apparently most of this time is "Waiting" time, with negligible send and receive time. I googled this problem with no lock. What I'm asking is what exactly is this "Waiting" time? Is the problem from IIS server? is something wrong with my code?
What I have tried:
i've tried commenting the c# code so it returns an empty string, but still there is a waiting time (although it's lesser this time: 5~6 sec).
唯一的原因是Ajax请求可能慢是因为你正在检索Plant对象然后在Json序列化,整个对象图ex:Plant - > LinkToPlant也是序列化的。所以你应该指定应该序列化的内容ex:
Oone reason that Ajax request may be "slow" is because you're retrieving the Plant object and then on Json serializing, the whole object graph ex: Plant -> LinkToPlant are also serialized. So you should specify what should be serialized ex:
//create a dynamic object and select fields
var obj =
{
ID = plant.ID,
Name = plant.Name
OtherField = plant.LinkToPlant.SomeField
};
string plantJson = JsonConvert.SerializeObject(obj);
关于Ajax代码,用错误替换失败(似乎无效或至少我从未听说过):
About the Ajax code, replace "failure" (doesn't seem valid or at least I never heard of it) with "error":
这篇关于Jquery ajax漫长的等待时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!