0

I'm trying to call a webservice and always get an error, the alert error shows 'undefined'

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Test()
{ 
 JavaScriptSerializer js = new JavaScriptSerializer(); 
 return js.Serialize("Hello");
}

this is the script

$.ajax({ type: "POST",
 contenttype: "application/json; charset=utf-8",
 data: "{}",
 url: "WorkflowAjaxHelper.asmx/Test",
 dataType: "json",
 async: false,
 success: function (res) {
 alert('success');
 },
 error: function (err) {
 alert(err.text);
 }
});
Rich
9001 gold badge9 silver badges18 bronze badges
asked Jul 19, 2012 at 14:11
2
  • Shouldn't you be looking at err.responseText? JS debugger very handy for this. Commented Jul 19, 2012 at 14:14
  • Thanks Paul, when changing this the error is 'the Test webservice name is not valid' Commented Jul 19, 2012 at 14:28

2 Answers 2

1

Text is not valid on the error object. You could use, any of the following, to get more info:

responseText 
status 
statusText

Leverage the debuggers built into Chrome, IE, or Firefox to help debug. You can also console.log an object and chrome and firefox are nice enough to let you click through the object model to see what is available.

answered Jul 19, 2012 at 14:27
Sign up to request clarification or add additional context in comments.

4 Comments

when changing this the error is 'the Test webservice name is not valid'
@jorgechess - Are you sure it should be a static method, I was having a similar issue because I had moved a method from a Page to an asmx and web methods in pages need to be static because a page instance is not created because of the overhead?
Can you view the service in the browser if you use the url for the service e.g. localhost/ApplicationName/WorkflowAjaxHelper.asmx? Is that url in the javascript correct? You should be able to view that service in the browser and test invoke from there.
I've changed the ResponseFormat to XML and the datatype: xml and worked! thanks!
0

Does the top of your .asmx.cs file have the following attributes, Jorge?

 /// <summary>
 /// Summary description for PartServices
 /// </summary>
 [WebService(Namespace = "http://tempuri.org/")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 [System.ComponentModel.ToolboxItem(false)]
 // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 // This bit here!!!
 [System.Web.Script.Services.ScriptService]
 public class PartServices : System.Web.Services.WebService
answered Jul 19, 2012 at 14:34

1 Comment

I've changed the ResponseFormat to XML and the datatype: xml and worked! thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.