Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using jQuery ajax to call a webmethod in my asp.net page, here's the code:

JavaScript
$.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);
            }
        });

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:
C#
    [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;
}

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).
Posted
Updated 14-Jul-17 17:07pm
v2
Comments
Richard Deeming 14-Jul-17 9:35am    
I'd start by removing the double JSON encoding/decoding, and wrapping your context class in a using block:
[WebMethod]
public static Plant GetPlantInfo()
{
    using (var dc = new  CMMS_MyDataClassesDataContext())
    {
        return dc.Plants.FirstOrDefault(x => x.Name == "someName");
    }
}

success: function(response){
    var info = response.d;
    $("#PlantName").val(info.Name);
    ...
},

1 solution

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:

C#
//create a dynamic object and select fields
 var obj = 
{
   ID = plant.ID,
   Name = plant.Name
   OtherField = plant.LinkToPlant.SomeField
};
string plantJson = JsonConvert.SerializeObject(obj);



About the Ajax code, replace "failure" (doesn't seem valid or at least I never heard of it) with "error":

$.ajax({
            failure: function (r) {
                alert(r.d);
            }
        });

to
$.ajax({
            error: function (r) {
                alert(r.d);
            }
        });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900