Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
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 = {};
//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 17-Oct-12 0:17am
soumyaraj1.1K
Edited 18-Oct-12 4:06am
(no name)105.2K
v3
Comments
soumyaraj at 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)

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100