Tag Archives: AJAX

Code Optimization for Ajax JQuery Web Request

When we use ajax calls for consuming web methods we use the following method.

$.ajax({
type: “POST”,
url: url,
data: JSON.stringify(params),
contentType: “application/json; charset=utf-8”,
dataType: “json”,
success: function (result) {
if (result == null || result == undefined || result.d.trim() == ”) return;
successFunc(JSON.parse(result.d), targetObj);
},
failure: function (error)
{ }
});

 

But to use this code on each page and for each web method call is a repetition of  code and we can avoid that if we define it at one place and then consume it from there every time we need to use it.

One way of doing it is to enclose the code in a function like this below.

function ajax_data_loader(url, params, successFunc, targetObj) {
$.ajax({
type: “POST”,
url: url,
data: JSON.stringify(params),
contentType: “application/json; charset=utf-8”,
dataType: “json”,
success: function (result) {
if (result == null || result == undefined || result.d.trim() == ”) return;
successFunc(JSON.parse(result.d), targetObj);
},
failure: function (error)
{ }
});
}

So wherever we need to use it we only need to call the function with the appropriate parameters.

ajax_data_loader(‘FleetInspectionAnalysis.aspx/GetTreeFirstLevel’,{customer_auto:customer_auto},load_firstlevel,$(‘#fleet_tree’));

 

 

Advertisements

An element with id ‘form1’ could not be found. Script error on page load

When you use Jquery and AJAX on the same form and you included the jQuery file with the <script /> this error will be thrown by VS (specially VS 2010).  To solve this issue just replace the <script /> with <script><script/> tag and it will solve your problem.

Note: This error will only come when you use IE, with every other browser this error will not come, but the thing common on all browsers is that design view is corrupted and the events will not fire as expected or not fire at all