Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
Return value from WebMethod to javascript getting undefined error?
 
here iam using webmethod call in js file like
GetFields(fieldName, _ID, myEditor.onGetFieldsCompleted, myEditor.onGetFieldsFailed);
webservice.cs file
[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...............
{ 
if (result != null) { 
if(result.length > 0) { 
} 
else { 
 
} 
}
pls help me out.......
Posted 9 Oct '12 - 2:48
Edited 9 Oct '12 - 2:54

Comments
Tejas Vaishnav - 10 Oct '12 - 0:53
please also refer newly added urls, it might help you out.

2 solutions

You can try to use System.Collection.Generic.List for the return value of your WebMethod.
  Permalink  
Comments
lakshmichawala - 9 Oct '12 - 9:36
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.
 
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[^]
  Permalink  
Comments
lakshmichawala - 9 Oct '12 - 9:39
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:49
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:14
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:54
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,356
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid