mautilus / sdk

MAUTILUS SmartTV SDK
BSD 3-Clause "New" or "Revised" License
99 stars 43 forks source link

jQuery does not work #12

Closed vnaira closed 8 years ago

vnaira commented 8 years ago

Dear please help. I can't organize jQuery Ajax call. how can I render $(document).ready(function(){

}) thanks in advance

streitdaniel commented 8 years ago

Hi vnaira, What code you want to process in function ready?

vnaira commented 8 years ago

I want to organise ajax request

streitdaniel commented 8 years ago

Hi vnaira, You can to do it for example in file app.js in init section or in scenes activate function. Use callback or promise to proceed this, because Ajax request is asynchronous function.

Best Regards Daniel Streit

vnaira commented 8 years ago

Thanks for quick response. I found that $.ajax did not work nowhere

streitdaniel commented 8 years ago

Try to use http://smarttv.mautilus.com/SDK/#!/api/Ajax-method-request

Regards Daniel Streit

vnaira commented 8 years ago

thanks a lot

SoCoxx commented 8 years ago

Using $(document).ready(function(){}) inside applications is not recommended, as this is already handled by SDK - in Main class. So when SDK loads your 1st scene, it is already passed the onload event.

As Daniel wrote, use Ajax class that wraps jQuery's $.ajax() method. Like:

var promise = Ajax.request("http://some.server.com/data-provider.php", {
    type: 'json',
    timeout: 5000,
    data: {
        "foo":"bar"
    },
}).done(function(data, status, jqxhr) {
    // data from server
    // text status
    // jquery xmlhttpresponse object
}).fail(function(jqxhr, status, error) {
    // jquery xmlhttpresponse object
    // text error
    // error thrown
});

Ajax.request returns Promise object, that can be chained to other promises to hold processing until all promises are resolved As bonus, all requests made through this class are visible in console - enabled in config.js:

developer: {
    debug: true,
    active: true,
    console: null
},
vnaira commented 8 years ago

Thank you very much

vnaira commented 8 years ago

Hi, could you send me an example how render JSON data from request to scene?

streitdaniel commented 8 years ago

Hi vnaria, Example for JSON data is in the RSS Reader demo, which is included.

SoCoxx commented 8 years ago

Use my Promise example from above - just add logic into .done(function(data, status, jqxhr) { }, i.e.

When AJAX response is json: {"foo": "bar"} and you have <div id="example"></div> in your scene, then

....done(function(data, status, jqxhr) {
   $('#example').html(data.foo);
}

will make <div id="example">bar</div>

I recommend using things like AJAX requests in Snippets, rather than in Scenes.

vnaira commented 8 years ago

The response data in my example is object. I want to send all data to html and then in html page read data with for()

SoCoxx commented 8 years ago

Why would you want to do that? In scene, you have data from response and access to the document object model. So you can do whatever you want right in the Ajax response handler

vnaira commented 8 years ago

Hi all, how to send headers with ajax, could you send an example please.

Thanks

SoCoxx commented 8 years ago

Ajax method request can handle headers:

var promise = Ajax.request("http://some.server.com/data-provider.php", {
    type: 'json',
    timeout: 5000,
    data: {
        "foo":"bar"
    },
    headers: {
        "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
        "Accept-Language": "en-gb, en;q=0.7"
    }
});

Ajax class is just a wrapper around std jQuery.ajax(), providing some extra features like Promises. Please read the class code in js/core/ajax.js to better understand how it works.

stenlik commented 8 years ago

Hi,

Did the answer help you? If yes please close the issue.

Thanks Mautilus SmartTV SDK Team