Closed vnaira closed 8 years ago
Hi vnaira, What code you want to process in function ready?
I want to organise ajax request
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
Thanks for quick response. I found that $.ajax did not work nowhere
Try to use http://smarttv.mautilus.com/SDK/#!/api/Ajax-method-request
Regards Daniel Streit
thanks a lot
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
},
Thank you very much
Hi, could you send me an example how render JSON data from request to scene?
Hi vnaria, Example for JSON data is in the RSS Reader demo, which is included.
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.
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()
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
Hi all, how to send headers with ajax, could you send an example please.
Thanks
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.
Hi,
Did the answer help you? If yes please close the issue.
Thanks Mautilus SmartTV SDK Team
Dear please help. I can't organize jQuery Ajax call. how can I render $(document).ready(function(){
}) thanks in advance