Question

I am using a handler to act as a proxy between a server with a string (actually a xml but I am trying for a string) and my Silverlight app. I have written the handler and it properly collects the string(xml). The problem I am having is converting that string from the JSON into a string that javascript can pass back to my Silverlight code.

Javascript:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var xmlReturn = new String("");
    function xmlStart() {
        $.getJSON('xmlProxy.ashx', function (data) {
            setXml(data);
        });
    }
    function setXml(data) {
        xmlReturn = data;
    }
    function getXml() {
        alert(xmlReturn);
        return xmlReturn;
    }

Silverlight:

private void button1_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Invoke("xmlStart");
    string test = (String)HtmlPage.Window.Invoke("getXml");

    textBox1.Text = test;
}

Just in case the handler code (baseurl taken out for security):

namespace HttpHandler_Proxy
{
    public class xmlProxy : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            WebClient getCap = new WebClient();

            string baseurl = "some_url";
            string response = getCap.DownloadString(baseurl);

            context.Response.ContentType = "application/json";

            context.Response.Write(response);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

I am relativity new to both Javascript and jQuery so this may be a trivial question and for that I apologize. On this version of the code it never sets xmlReturn to anything other than ""

I have done other versions but the code is always returned to Silverlight as null/undefined/"".

Était-ce utile?

La solution

Your content type is set to json, but you don't seem to be doing any encoding, i.e. turning the response from the server into valid json. Try adding something like:

response = new JavaScriptSerializer().Serialize(response);

Autres conseils

Why not try using $.load instead of getJSON if you don't intend on treating that string as json at that point.

Edit

First, you should check the value of data inside your success callback (console.log(data)). Make sure your server-side code is returning what you intend it to.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top