locked
No return value from javascript if the js function contains jquery RRS feed

  • Question

  • I am not able to return a value from js if that js function contains jquery.
    This works fine if the js function does not contain jquery:
    Silverlight:
    string s = (string)HtmlPage.Window.Invoke("jsfunction",null);

    js: function jsfunction(){
    return 'value';
    }

    However, if i include jquery in the javascript function it is not returning a value. This js function calls a WCF REST method.

    Silverlight:
    string s = (string)HtmlPage.Window.Invoke("jsfunctionwithjquery",null);

    js: function jsfunctionwithjquery(){
    var restmethod = "restservice.svc/test"
    $.ajax({
    url: restmethod,
    type: "GET",
    contentType: "application/json",
    success: function(data) {
    return 'success';
    },
    failure: function(data) {
    return 'failure';
    },
    complete: function(data) {
    return 'complete';
    }
    });
    }
    The returned value from this jsfunctionwithjquery is null. I know the rest method is being called because i can break in that method when debugging. It appears as though the Invoke returns before the jquery has been executed. Any ideas how to make Window.Invoke wait for the jquery to complete and return the value specified?
    Friday, June 25, 2010 9:22 AM

Answers

  • A coworker pointed out a fix for me. Adding the async: false to jquery fixes my issue
    function jsfunctionwithjquery(){
    var restmethod = "restservice.svc/test"
    $.ajax({
    url: restmethod,
    type: "GET",
    contentType: "application/json",
    async: false,
    success: function(data) {
    return 'success';
    }
    });
    }
    Monday, June 28, 2010 11:44 AM

All replies

  •  INstead of let the indow invoke to wait.

     I think you may use javasript code to call managed code.

     Simulate an async manner

     

    Monday, June 28, 2010 3:03 AM

  •  You are calling an async method of a service and catching the callback from server on error or success.

     

    Your jsfunctionwithjquery() does not return a value. The 'complete' and 'failure' value is returned by other functions (builded in the ajax call). Try to add return "ajax call fired" in the end of your function. When you invoke your function you should get the "ajax call fired" result.

     If you want to use somekind of service you can fire it up in Silverlight, code behind. Just add reference to a webService and use it.

    Monday, June 28, 2010 4:57 AM
  • Yes, you're right if i put return "ajax call fired" at the end of the function I will get "ajax call fired" as a result of invoking the js function. However, I ultimately want to return the json that is created in the service call.
    For instance:
    Silverlight:
    string s = (string)HtmlPage.Window.Invoke("jsfunctionwithjquery",null);
    js:
    function jsfunctionwithjquery(){
    var restmethod = "restservice.svc/test"
    $.ajax({
    url: restmethod,
    type: "GET",
    contentType: "application/json",
    success: function(data) {
    return data.d; //json data that i would like to return to the calling C# method
    }
    });
    }
    Monday, June 28, 2010 7:42 AM
  •  Try to do that in a stupid way:

    function jsfunctionwithjquery(){
    var restmethod = "restservice.svc/test"
    GLOBAL_WAIT = 0;
    $.ajax({
    url: restmethod,
    type: "GET",
    contentType: "application/json",
    success: function(data) {
    GLOBAL_WAIT = 1;
    GLOBAL_VAR = data;
    return data.d; //json data that i would like to return to the calling C# method
    }
    });
    while (GLOBAL_WAIT == 0) {} --> stupid waiting loop
    return GLOBAL_VAR;
    }

     In this way you will call async method and wait (it will block your Silverlight application) for the result from server side. When result comes you unblock your loop and return the result of the ajax call.

     A better way to do that would be to call C# method from JS when the callback comes to you.

    Read this: Calling managed code from JS

     On the silverlight side:

    [ScriptableMember()]
    public void RetrieveData(object Json)
    {
    //Do work with Json object

    }
    On JS side: 
    function jsfunctionwithjquery(){
    var restmethod = "restservice.svc/test"
    $.ajax({
    url: restmethod,
    type: "GET",
    contentType: "application/json",
    success: function(data) {
    var control = document.getElementById("Control");
    control.content.MyControl.RetrieveData(data);
    }
    });
    }
     Remember to register class as a scriptable object in the start up of app.
    Now you fire your JS func with the ajax call from C#, next JS will ask server for data and fire a callback when data comes, the callback in JS fires your C# method. But still I would recommend you to get data from a service using Silverlight and WCF because you can avoid using JS.
    Monday, June 28, 2010 8:07 AM
  • A coworker pointed out a fix for me. Adding the async: false to jquery fixes my issue
    function jsfunctionwithjquery(){
    var restmethod = "restservice.svc/test"
    $.ajax({
    url: restmethod,
    type: "GET",
    contentType: "application/json",
    async: false,
    success: function(data) {
    return 'success';
    }
    });
    }
    Monday, June 28, 2010 11:44 AM
  • This is not longer valid... whatwg.org . Apparently we are only left with the stupid way. Whatwg organization apparently love to have codes running without any sense or value to wait for.
    Tuesday, March 8, 2016 2:48 PM