Question

I am learning javascript and was trying out some code below. I am calling the below javascript object as

var client=new atrmsClient('6247543');

var val=client.getRosterData();

I get an error on this line:

var postData= {"cmdShow": "Show", "txtEmpId" : EmpId, "txtPeopleSoft_Id": EmpId, "__VIEWSTATE":  viewState   }

EmpId is undefined. Can anyone tell me what I am doing wrong ?

"use strict";

function atrmsClient(EmployeeId)
{
    this.EmpId=EmployeeId;

    var siteUrl="http://wncrpma011.japa.ad.aexp.com/TransportRoster/EmployeeReport.aspx";


    var getViewState=function()
    {

            $.ajax
        ({
            type: "GET",
            url: siteUrl,
            dataType: 'html',           
            processData: false,
                xhrFields: 
                {
                    withCredentials: true
                }
        })
        .done(ExtractViewState).fail(errorFunc).always(alwaysFunc);

        return "";

    };

    var SendPostRequest=function(viewState)
    {
        var postData= {"cmdShow": "Show", "txtEmpId" : EmpId, "txtPeopleSoft_Id": EmpId, "__VIEWSTATE":  viewState   }

            $.ajax
        ({
            type: "POST",
            url: siteUrl,
            data: postData,
            dataType: 'html',           
            processData: false,
                xhrFields: 
                {
                    withCredentials: true
                }
        })
        .done(parseRosterData).fail(errorFunc).always(alwaysFunc);


    };


    var parseRosterData=function(data)
    {
        console.log(data);



    };

    var ExtractViewState=function(data)
    {

        var rawResponse=data;
        var viewState=$(rawResponse).find('input[name=__VIEWSTATE]')[0].value;
        console.log(viewState);

        SendPostRequest(viewState);

    };

    var errorFunc=function()
    {


    };


    var alwaysFunc=function()
    {


    };

    this.getRosterData=function()
    {
        var viewStateVal=getViewState();
        console.log("calling");
        return "";
    };



}
Was it helpful?

Solution

You never declare a variable called EmpId.

The only EmpId you have is a property of the atrmsClient instance.

Add

var EmpId = EmployeeId;

… or just use EmployeeId as that is still in scope.

OTHER TIPS

Another solution could be:

function atrmsClient(EmployeeId) {

    var that = this;
    this.EmpId=EmployeeId;

    var SendPostRequest=function(viewState) {
        var postData= {"cmdShow": "Show", "txtEmpId" : that.EmpId, "txtPeopleSoft_Id": that.EmpId, "__VIEWSTATE":  viewState   }
        // ...
    };

}

It depends if you need EmpId to be available from outside, i.e. using client.EmpId. If yes, then this solution should suit your needs. Otherwise, use @Quentin's.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top