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);
}
});
-
Shouldn't you be looking at err.responseText? JS debugger very handy for this.Paul Alan Taylor– Paul Alan Taylor2012年07月19日 14:14:28 +00:00Commented Jul 19, 2012 at 14:14
-
Thanks Paul, when changing this the error is 'the Test webservice name is not valid'jorgechess– jorgechess2012年07月19日 14:28:00 +00:00Commented Jul 19, 2012 at 14:28
2 Answers 2
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
jorgechess
when changing this the error is 'the Test webservice name is not valid'
Paulie Waulie
@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?
Paulie Waulie
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.
jorgechess
I've changed the ResponseFormat to XML and the datatype: xml and worked! thanks!
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
jorgechess
I've changed the ResponseFormat to XML and the datatype: xml and worked! thanks!
lang-js