views:

201

answers:

3

I am using a proxy service to allow my client side javascript to talk to a service on another domain

The proxy is a simple ashx file with simply gets the request and forwards it onto the service on the other domain :

using (var sr = new System.IO.StreamReader(context.Request.InputStream))
            {
                requestData = sr.ReadToEnd();
            }

            string data = HttpUtility.UrlDecode(requestData);

            using (var client = new WebClient())
            {
                client.BaseAddress = serviceUrl;
                client.Headers.Add("Content-Type", "application/json");

                response = client.UploadString(new Uri(webserviceUrl), data);
            }

The client javascript calling this proxy looks like this

function TestMethod() {

    $.ajax({
        type: "POST",
        url: "/custommodules/configuratorproxyservice.ashx?m=TestMethod",
        contentType: "application/json; charset=utf-8",
        data: JSON.parse('{"testObj":{"Name":"jo","Ref":"jones","LastModified":"\/Date(-62135596800000+0000)\/"}}'),
        dataType: "json",
        success: AjaxSucceeded,
        error: AjaxFailed
    });

    function AjaxSucceeded(result) {
        alert(result);
    }

    function AjaxFailed(result) {
        alert(result.status + ' - ' + result.statusText);
    }
}

This works fine until I have to pass a date. At which point I get a Bad Request error when the proxy tries to call the service. That is to say, if I remove the ,"LastModified":"\/Date(-62135596800000+0000)\/" from the json data then the call succeed. Add it back in and it fails.

If I make the call from the same domain it is fine, it's only when it goes via the proxy that it fails.

I did have this working at one point but have now lost it.

Have tried using JSON.Parse on the object before sending. and JSON.Stringify, but no joy

anyone got any ideas what I am missing?

have also tried the customised parser methods mentioned in this article http://www.west-wind.com/Weblog/posts/896411.aspx

Any ideas?

+1  A: 

If you're POSTing the data, do you need the UrlDecode? If the plus sign gets through to the UrlDecode as is, I'm guessing it'll be translated to a space, which could be causing you a date format problem. Where exactly is the error you're getting coming from?

Matt Gibson
A: 

turns out this line is not neccessary

string data = HttpUtility.UrlDecode(requestData);
Christo Fur
A: 

Well, looks like your ashx is not passing the request/response straight through, right? It's changing it. I'm not a .net guy, and don't really know what your code is doing, but I'd say you should make sure it's doing as little as possible. One other thing to look at, when creating a proxy page, is whether you're sending the response straight through, or if you're loading it in memory in your proxy before you start sending it. Best way to do it, I believe, is to simply pass the data along as your proxy recieves it.

morgancodes