Click here to Skip to main content
15,913,261 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
C#
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
C#
if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
//Mvcdemo_post_send.calculation = (function () {
$('#Submit1').live('click', function (e) {
    var fisrtNumber = $('#txtfirstno').val();
    var secondNumber = $('#txtsecondno').val();
    var params = '{fisrtNumber=:' + fisrtNumber + '&secondNumber=' + secondNumber+'}';
//        Mvcdemo_post_send.common.showalert();
    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
Posted
Updated 18-Oct-12 3:06am
v3
Comments
soumyaraj 19-Oct-12 6:22am    
I could solve myself.here is the code in calculation.js

if (typeof Mvcdemo_post_send === 'undefined') Mvcdemo_post_send = {};
//Mvcdemo_post_send.calculation = (function () {
$('#Submit1').live('click', function (e) {

var fisrtNumber = $('#txtfirstNumber').val();
var secondNumber = $('#txtsecondNumber').val();
var params = 'fisrtNumber=' + fisrtNumber + '&secondNumber=' + secondNumber ;


Mvcdemo_post_send.common.GetData('/Home/AddNumbers', params, null, OnSuccessCall_calc, OnErrorCall_calc);
});
//});
var OnSuccessCall_calc = function (result) {

$('#txtAddNumbers').val(result);
}
var OnErrorCall_calc = function () {

alert(request.status + " " + request.statusText);

}

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