1

I'm trying to make webservices call from a jQuery mobile website on an ASP.NET base. The webservices take post parameter and return an XML. I cannot change the webservice side.

I'm facing the cross domain problem, and from my understanding, I need to use a proxy.

I'm thinking of using a generic handler but I don't know how to do that.

My services methods looks like that:

https://myserver.com/WCF/Info.svc/MyMehod1
https://myserver.com/WCF/Info.svc/MyMehod2

And take Post parameters

What would be a good way to do that in c#?

Thanks

Distwo
  • 11,569
  • 8
  • 42
  • 65

2 Answers2

2

Check this question out: Accessing web Service from jQuery - cross domain

As an alternative, you could also create an HttpHandler that you invoke with jQuery Ajax. The handler can then call the web service and write the output to the Http response.

Community
  • 1
  • 1
nick_w
  • 14,758
  • 3
  • 51
  • 71
  • I used the httpHandler alternative and it worked. I will post my code as soon as stackoverflow allow me, for those who will face the same problem ;) – Distwo Oct 09 '12 at 22:06
1

I finally got it to work.

for those who have the same problem,

on the client side, I have used a generic handler to do the webservice call and expose the result.

Handler sample:

public void ProcessRequest(HttpContext context)
{
    string method = context.Request.QueryString["method"].ToString().ToLower();

    if (method == "MyMethod1")
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(CallWebService(method, param));
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
    else if (method == "MyMethod2")
    {
        context.Response.ContentType = "text/plain";
        string param = "myparam";
        context.Response.Write(CallWebService(method, param));
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
    else
    {
    //context.Response.ContentType = "text/plain";
    //context.Response.Write("Hello World");
    }


}

private string CallWebService(string method, string param)
{
    string ServeurURL = "https://myserver.com"; 

    System.Net.WebRequest req = System.Net.WebRequest.Create(ServeurURL + method);
    req.Method = "POST";

    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(param);
    req.ContentType = "text/xml";
    req.ContentLength = byteArray.Length;
    System.IO.Stream reqstream = req.GetRequestStream();
    reqstream.Write(byteArray, 0, byteArray.Length);
    reqstream.Close();

    System.Net.WebResponse wResponse = req.GetResponse();
    reqstream = wResponse.GetResponseStream();
    System.IO.StreamReader reader = new System.IO.StreamReader(reqstream);
    string responseFromServer = reader.ReadToEnd();

    return responseFromServer;
}

jQuery/Ajax call:

jQuery(function() {
        $('#btn1').click(function (e) {
            e.preventDefault();
            jQuery.ajax({
                type: "GET",
                url: "MyHandler.ashx",
                data: "method=MyMethod1",
                success: function (data) {
                    $('#display').html("<h1>" + data.toString() + "</h1>");
                }
            });
        });
        $('#btn2').click(function (e) {
            e.preventDefault();
            jQuery.ajax({
                type: "GET",
                url: "MyHandler.ashx",
                data: "method=MyMethod2",
                success: function (data) {
                    $('#display').html("<h1>" + data.toString() + "</h1>");
                }
            });
        });
    });

And now it's working :)

Distwo
  • 11,569
  • 8
  • 42
  • 65
  • 1
    Hey @Distwo, glad that worked out for you. Just a tip: it's probably better etiquette in cases like this to to update your question with the answer you chose, rather than unaccept the answer. – nick_w Oct 13 '12 at 01:08
  • 1
    I thought I could mark both as accepted answered, your being the logic, and mine being the associated code, but it removed the first one... :( – Distwo Oct 15 '12 at 16:02