How can I modify my jQuery ajax response to do the same with JSON, what it currently does when returning JavaScript?

$('.showprayer').click( function(event) 
{
  $.post('/show_prayer', { 'name' : $(this).text() }, function(data) 
  {
      eval(data);
  });
});

The jQuery function above returns the following line , as the ajax response is returned from the server:

'#prayer_date'.html("03/19/1968");

Someone, having seen this code earlier, answered that "returning javascript instead of JSON is a terrible way of using Ajax".

My problem is that I thought this was the only way to do this.

So, if someone could enlighten me., If I wanted to do the same by returning JSON, that would look like it and it would include significantly more code than my current ajax answer, which changes the value very briefly item prayer_dateon its new date value?

+3
source share
2

JSON . , AJAX , , (html).

$('.showprayer').click( function(event) 
{
  $.post('/show_prayer', { 'name' : $(this).text() }, function(data) 
  {
      $('#prayer_date').html(data.Date);
  },'json');
});

:

"{ 'Date' : '03/19/1968' }"
+2

json, { "date": "03/19/1968" }, eval, do $("#prayer_date").text(data.date); ( html, ). , , , eval.

+1

Source: https://habr.com/ru/post/1771203/


All Articles