Hi All,
How can I use a common get,post method and pass the parameter from cshtml to calculation js file using object oriented JavaScript and JQuery.
Here is my common.js file and calculation.js file
common.js
if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
Mvcdemo_post_send.common = {
PostData: function (requestUrl, dataStringified, options, onSuccess, onError) {
var ajaxOptions = $.extend({}, Mvcdemo_post_send.common.DefaultOptions, options);
$.ajax({
cache: ajaxOptions.cache,
type: 'POST',
url: requestUrl,
data: dataStringified,
contentType: ajaxOptions.contentType,
dataType: ajaxOptions.dataType,
success: onSuccess,
error: onError
});
},
GetData: function (requestUrl, dataStringified, options, onSuccess, onError) {
var ajaxOptions = $.extend({}, Mvcdemo_post_send.common.DefaultOptions, options);
$.ajax({
cache: ajaxOptions.cache,
type: 'GET',
url: requestUrl,
data: dataStringified,
contentType: ajaxOptions.contentType,
dataType: ajaxOptions.dataType,
success: onSuccess,
error: onError
});
}
}
}
Mvcdemo_post_send.common.DefaultOptions = {
cache: false,
contentType: 'application/json; charset=utf-8',
dataType: 'json'
}
calculation.js
if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
$('#Submit1').live('click', function (e) {
var fisrtNumber = $('#txtfirstno').val();
var secondNumber = $('#txtsecondno').val();
var params = '{fisrtNumber=:' + fisrtNumber + '&secondNumber=' + secondNumber+'}';
Mvcdemo_post_send.common.GetData("Home/AddNumbers", params, null, OnSuccessCall, OnErrorCall);
});
var OnSuccessCall = function (result) {
$('#txtAddNumbers').val(result);
}
var OnErrorCall = function (request, status, error) {
alert(request.status + " " + request.statusText);
}
After debugging it goes to 'OnErrorCall' function
Thanks& Regards,
Soumya