Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Return value from WebMethod to javascript getting undefined error?

here iam using webmethod call in js file like
C#
GetFields(fieldName, _ID, myEditor.onGetFieldsCompleted, myEditor.onGetFieldsFailed);
webservice.cs file
C#
[WebMethod] 
public string[] GetFields(string mergeFieldName, Int32 PartnershipID) 
{ 
string[] stringArray = (string[])fields.ToArray(typeof(string)); 
return stringArray ; 
} 

here from webservice we are returning string array to javascript ......at tht time iam getting reult in javascript as undefined error ......pls see below js file code ........

onGetFieldsCompleted = function (result)----------here in result iam getting undefined.....actualy i have to get string array whcih i returned from webmethod in result but iam getting undefined in result...............
JavaScript
{ 
if (result != null) { 
if(result.length > 0) { 
} 
else { 

} 
}
pls help me out.......
Posted
Updated 9-Oct-12 2:54am
v2
Comments
Tejas Vaishnav 10-Oct-12 0:53am    
please also refer newly added urls, it might help you out.

You can try to use System.Collection.Generic.List<t> for the return value of your WebMethod.
 
Share this answer
 
Comments
lakshmichawala 9-Oct-12 9:36am    
i have to return string array itself ....not generic list...iam not getting y it is working fine from local code ...but after deployment in test server iam getting return value from webthod to javascript as undefined where as from local code iam able to return value in result ....y not from test server after deployment....
first of all you can not call your webservice method by simply write it's name and passwing parameter list like this....

GetFields(fieldName, _ID, myEditor.onGetFieldsCompleted, myEditor.onGetFieldsFailed);

you can call your serivce by using one of the most common method of jquery $.ajax() like this.

C#
function callWebService() {
var _data = "{mergeFieldName:'YOUR VALUE',PartnershipID:'YOUR VALUE'}";
var _url = "YOUR SERVICE URL"
$.ajax({
    type: "POST",
    url: _url,
    data: JSON.stringify(_data),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg);
    },
    error: function (msg) {
        alert('error' + msg);
    }
});



for more detail on how to call webservice method using javascript or jquery please refer this links

Jquery to call web services[^]
how to call webservice using jquery[^]


Consuming Webservice[^]
How to call a Web Service from client-side JavaScript using ASP.NET AJAX[^]
 
Share this answer
 
v3
Comments
lakshmichawala 9-Oct-12 9:39am    
i want in javascript not jquery...iam not getting y return value from webmethod to javascript is undefined....it works fine from local code after deployment in test server iam getting return value as undefined from webmethod to javascript ...
Tejas Vaishnav 10-Oct-12 0:49am    
So where is you javascript code, you have used to call your webservice method.
please use improve question functionality and attache your javascript code so we can identify your problem.
lakshmichawala 10-Oct-12 4:14am    
the below line of code is used to call web service method in js file ..

html.EditorDocument.attachEvent( 'onkeyup', myEditor.showSuggestion ) ; //Display drop down list

myEditor.showSuggestion = function()
{
var fieldName = "P";
var ID="2";
GetFields(fieldName, _ID, myEditor.onGetFieldsCompleted, myEditor.onGetFieldsFailed);


}

/LISTNER WHEN THE WEB SERVICE CALL IS RETURNED SUCCESSFULLY
myEditor.onGetFieldsCompleted= function (result)
{

if (result != null)
{
if(result.length > 0)---here in result iam getting undefined
{
do something
}
else
{
do something

}
}
}



Tejas Vaishnav 11-Oct-12 2:54am    
you can not call your webservice method directly like you call your javascript function.. suppose function a(){ alert('test');} and you call it in your function b() {a();}; this is not possible while calling your remote method of your webservice.

you need to call your web method using Ajax request or HTTPRequest from code behind.

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