Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var Even = myfunction();

function myfunction() {
 var objTask = {
        SenderID: parseInt(sessionStorage.getItem('UserID')) //userid
    }
    var pass =[];
$.ajax({
        type: "POST",
        url: "http://localhost:49534/TaskManagement.svc/TaskList",
        data: JSON.stringify(objTask),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data, status, jqXHR, result) {
        debugger;
        for (i = 0; i < data.length; i++) {
              pass.push ({"title": data[i].TaskName});
            }
        }
    });
    alert("Loding...");
   return pass;
}


What I have tried:

when the above method is writing in ajax call ...and the method not call for myfunction();

I m trying to add "alert("some text"); " the line end for the my function then they call successfully....and the same time i remove the alert box,function not calling...

plz help...
Posted
Updated 15-Dec-16 2:10am

1 solution

You are calling MyFunction before it has been defined, so move to call to after it has been defined.

JavaScript
function myfunction() {
 var objTask = {
        SenderID: parseInt(sessionStorage.getItem('UserID')) //userid
    }
    var pass =[];
$.ajax({
        type: "POST",
        url: "http://localhost:49534/TaskManagement.svc/TaskList",
        data: JSON.stringify(objTask),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data, status, jqXHR, result) {
        debugger;
        for (i = 0; i < data.length; i++) {
              pass.push ({"title": data[i].TaskName});
            }
        }
    });
    alert("Loding...");
   return pass;
}

var Even = myfunction();
 
Share this answer
 
Comments
venkatesh (chennai) 15-Dec-16 8:17am    
not working ...
F-ES Sitecore 15-Dec-16 8:32am    
learn to debug your code then, "not working" means nothing, it does provide any information anyone can use to help.
venkatesh (chennai) 15-Dec-16 8:40am    
i am try to debug ..i m not found any errors .....
venkatesh (chennai) 15-Dec-16 8:41am    
they function working when i add alter box...other wise not call this function not working....
F-ES Sitecore 15-Dec-16 8:52am    
The ajax call doesn't wait until the response comes back so it doesn't halt your code. You make the ajax call then immediately return "pass" which is going to be an empty array, it doesn't wait for the success event to fire and populate the array. If that is what you want then make sure the ajax call as synchronous, so set the async option to false in your ajax call, consult jquery docs for exact syntax.

http://api.jquery.com/jquery.ajax/

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