Reputation: 10412
I am working on an ASP.NET web application.
I want to use Jquery ajax to call a web service. The webservice will return a string.
This is my webservice:
[WebService(Namespace = "http://localhost/")]
[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.
[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public string GetStringForPerson(string Personname, int StartYear, int EndYear)
{
string S = "Hello" + Personname + " You where born between " + StartYear + " and " + EndYear;
return S;
}
}
And the Ajax function that will call it:
function GetStringForPerson(Name) {
$.ajax({
type: "POST",
url: "Service/MyWebService.asmx/GetStringForPerson",
dataType: "json",
data: "{'Personname':Name, 'StartYear':'2001', 'EndYear':'2005'}",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
alert(XMLHttpRequest.status);
alert(errorThrown);
}
});
}
But when I call it, I am getting an error 500, "internal Server Error"
I placed a break point in the web method but it is not reaching it.
What might be the problem?
Thanks
Upvotes: 0
Views: 1261
Reputation: 66649
One bug that I see is the Name
variable, is not send as string and not as variable as it is.
So change this line as :
data: "{'Personname':'" + Name + "', 'StartYear':'2001', 'EndYear':'2005'}",
Upvotes: 1