开发者

Mono: How can I consume a web service with javascript and jquery?

sorry for my English

I created a website with some aspx pages, for example, Default.aspx, and a web service called DataService.asmx. From the Default.aspx page I would like to call the web service using jquery. The problem is that I can not call the web service using jquery

I'm using mono 2.4.4 on ubuntu server version 10.04.

How can I consume a web service with javascript and jquery?

Please help me, thanks

Using firebug I see

Object reference not set to an instance of an object
Description: HTTP 500. Error processing request.
Stack Trace: System.NullReferenceException: Object reference not set to an instance of  an object at System.Web.Script.Services.WebServiceData.GetWebServiceData  (System.Web.HttpContext context, System.String virtualPath, Boolean failIfNoData, Boolean PageMethods) [0x00000 ] at System.Web.Script.Services.WebServiceData.GetWebServiceData (System.Web.HttpContext context, System.String virtualPath) [0x00000] at System.Web.Script.Services.RestHandler.CreateHandler (System.Web.HttpContext context) [ 0x00000] at System.Web.Script.Services.RestHandlerFactory.GetHandler (System.Web.HttpContext context, RequestType System.String, System.String url, pathTranslated System.String) [0x00000] at System.Web.Script.Services.ScriptHandlerFactory . GetHandler (System.Web.HttpContext context, RequestType System.String, System.String url, pathTranslated System.String) [0x00000] at System.Web.HttpApplication.GetHandler (System.Web.HttpContext context, System.String url, Boolean ignoreContextHandler) [0x00000] at System.Web.HttpApplication.GetHandler (System.Web.HttpContext context, System.String url) [0x00000] + at System.Web.HttpApplication <Pipeline> c__Iterator2.MoveNext () [0x00000]

Version Information: Runtime: Mono 2.4.4 ASP.NET Version: 2.0.50727.1433

Web Service Code (DataService.asmx)

[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo WsiProfiles.BasicProfile1_1 =)]
[ScriptService ()]
public class DataService: System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod (ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
    public string SayHello ()
    {
        return "Hello !!!";
    }

}

Javascript (Default.aspx)

$ (Document). Ready (function () {

        $.Ajax ({
            type: "POST",
            url: _webServiceAddress + method,
            Date: parameters,
            contentType: "application / json, charset = utf-8",
            dataType: "json",
            success: function (data) {alert (data);}
            error: function () {alert ("Error");}
        });

    });


This is the JavaScript code. The other was wrong, sorry

$(document).rea开发者_运维知识库dy(function () { 
        $.ajax({ 
            type: "POST", 
            url: "DataService.asmx/SayHello", 
            data: "{}", 
            contentType: "application/json; charset=utf-8", 
            dataType: "json", 
            success: function () { alert("Ok"); }, 
            error: function () { alert("Error"); } 
        }); 
    });


Your javascript is incorrect:

$ (document). ready (function () {

    $.ajax ({
        type: "POST",
        url: _webServiceAddress + method,
        data: parameters,
        contentType: "application/json,charset=utf-8",
        dataType: "json",
        success: function (data) {alert (data);},
        error: function () {alert ("Error");}
    });

});

Take note of the capitalization, data not date, and a comma seperating success and error.


Currently I think is not possible to call from ajax calls to MyService.asmx/MyMethod. 500 is always returned. I'me pursuing a solution for weeks and right now I'm replacing .asmx with ServiceStack.NET based services.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜