Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: return values to load callback function

Tags:

html

jquery

It's required to partial upload Html page with $(selector).load('/sitePath', params, function() { ... }); method. Html layout comes ok but it would be nice to return some additional values for javascript also.

Nowadays I return special javascript block with javascript variables inside html layout to read their values on load callback. What is the best practice to perform such operation?

Thank you in advance!

like image 701
Andrew Florko Avatar asked Nov 14 '22 08:11

Andrew Florko


1 Answers

I'd go for JSON and .ajax().

$.ajax({
   url:      '/sitePath',
   dataType: 'json',
   type:     'POST',
   data:     params,
   success:  function(data){
       $(selector).html(data.newcontent);
       var info = data.additionalinfo;
   }
});

Of course assumes that you create a JSON string on your server that helds both, a property named newcontent which should contain html markup and a property named additionalinfo.

like image 174
jAndy Avatar answered Dec 17 '22 12:12

jAndy