I found some other questions about this, but they didn’t work for me 🙁 So I’m posting my own question:
I’ve made an ASP.NET webservice in mono. The code for it is:
using System;
using System.Web;
using System.Web.Services;
namespace testWebservice
{
public class ws : System.Web.Services.WebService
{
public ws() {
}
[WebMethod]
public String sendCar()
{
return "44.435006,26.102314";
}
}
}
I’m trying to call this webservice from a website using jQuery:
function getCar()
{
$.ajax( {
type:'POST',
url:'http://127.0.0.1:8080/ws.asmx/sendCar',
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success:function(msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
})
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
}
When I run the page I get an alert saying “0 error”.
Cay anyone give me some advice please?
For the service to work you need to:
Add to your project a reference to
System.Web.Extension.Add a
using System.Web.Script.Services;to your ws source.Add:
to your ws class.
Add:
inside your web.config under the
<httpHandlers>tag.If you are calling
getCar()from a button click addreturn false;after the method call so that the click will not cause a postback.For example:
[Default.aspx]
[ws.asmx.cs]
[web.config]