0

a small but very annoying problem. I am trying to use jQuery JSON call with asp.net web service. It all works fine with the below HttpModule if I call the web service without any arguments. As soon as I try and send a value from the client side, Module gets executed but the process doesn't pass over to the actual webservice method and returns with a server side 500 error. If we remove the module from the middle, then the method gets executed perfectly fine with parameter but then the response comes back in the XML format instead of JSON so we are helpless to use the Module.

----------- Jquery Call ---------------------

    var dd = { 'name': 'pakistan' };

    $(document).ready(function () {
        $.getJSON("http://localhost:59271/Testing/shows-app.asmx/HelloWorld?callback=?",
        dd,                    
        function (data) {
            val = JSON.parse(data.d)
            $("#content").html(val.response);
        });
    });

------------ HttpModule -------------

    private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";

    public void Dispose()
    {
    }
    public void Init(HttpApplication app)
    {
        app.BeginRequest += OnBeginRequest;
        app.EndRequest += new EventHandler(OnEndRequest);
    }
    public void OnBeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpRequest request = app.Request;
        //Make sure we only apply to our Web Service
        if (request.Url.AbsolutePath.ToLower().Contains("-app."))
        {
            var method = app.Context.Request.Headers["REQUEST_METHOD"];

            if (string.IsNullOrEmpty(app.Context.Request.ContentType))
            {
                app.Context.Request.ContentType = JSON_CONTENT_TYPE;                    
            }

            app.Context.Response.Write(app.Context.Request.Params["callback"] + "(");

            var method2 = app.Context.Request.Headers["REQUEST_METHOD"];

        }
    }
    void OnEndRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpRequest request = app.Request;
        if (request.Url.AbsolutePath.ToLower().Contains("-app."))
        {
            app.Context.Response.Write(")");
            app.Context.Response.ContentType = "application/json";
        }
    }

----------- Webservice ---------------------

[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string HelloWorld(string name)
{
    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new
    {
        response = "Pakistan " + " Zindabad"
    });

    return json;

    //string jsoncallback = HttpContext.Current.Request["callback"];
    //return string.Format("{0}({1})", jsoncallback, json);
}

Please remember that, if we remove the module from the middle, then the method gets executed perfectly fine with parameter but then the response comes back in the XML format instead of JSON so we are helpless to use the Module.

Thanks a bunch in advance.

Jibran
  • 11
  • 1
  • 2
  • ASP.NET will not return JSON from a `GET` request as far as I know. You must use `POST`. If cross-domain requests are required, then look into CORS and the required headers to make that work – BLSully Sep 25 '12 at 13:23
  • I doubt if jsonp/cross domain accepts arguments through post? I believe $.getJson() sets it to GET by default. – Jibran Sep 25 '12 at 13:43
  • see solution here http://stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery – BLSully Sep 25 '12 at 13:54
  • wouldn't work for asp.net web services actually. – Jibran Sep 25 '12 at 14:12
  • This would beg to differ: http://encosia.com/using-cors-to-access-asp-net-services-across-domains/ (with the caveat that client must be >IE9 (or use ChromeFrame), Chrome, Safari, or FF)...but that's not an ASP.NET limitation. – BLSully Sep 25 '12 at 14:56

0 Answers0