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.8K
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 DamithSL 265
1 CPallini 235
2 OriginalGriff 233
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web03 | 2.8.141216.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