Search code examples
phpjqueryajaxjsonjplayer

Why I can not send json string to php with jquery?


I'm trying to store the following data, taken from a html

  title:"Tempered Song",
  artist:"Miaow",
  mp3:"http://www.jplayer.org/audio/mp3/Miaow-01-Tempered-song.mp3",
  oga:"http://www.jplayer.org/audio/ogg/Miaow-01-Tempered-song.ogg",
  poster: "http://www.jplayer.org/audio/poster/Miaow_640x360.png"

my html code is:

<a class="add-music" data-title="Las Voces" data-artist="" href="audios/song-little.mp3">Add to List</a>
<a class="download-music" href="audios/song-little.mp3">Download</a>

<a class="add-music" data-title="Las Voces del Bosque" data-artist="" href="audios/song-little.mp3">Add to List</a>
<a class="download-music" href="audios/song-middle.mp3">Download</a>

<a class="add-music" data-title="Las Bosque" data-artist="" href="audios/song-little.mp3">Add to List</a>
<a class="download-music" href="audios/song-big.mp3">Download</a>

and my code jquery is:

 $( document ).ready(function() {
 $('.add-music').click(function() {
    $.ajax({
        'type':'POST',
        'data':fuction() {
          var songNew = JSON.stringify({
            title: $(this).attr('data-title'),
            artist: $(this).attr('data-artist'),
            mp3: $(this).attr('href'),
          });
        });
        datatype: 'json',
        url: 'session.php',
        async: true,
        cache:false
        });
    });
 });

but it does not work, is there any way to make this better and cleaner?


Solution

  • in the end it worked out well

    $(document).ready(function () {
         $('.add-music').click(function () {
             var songNew = JSON.stringify({
                 title: $(this).attr('data-title'),
                 artist: $(this).attr('data-artist'),
                 mp3: $(this).attr('href')
             });
             var songIE = {json:songNew};
             $.ajax({
                 type: 'POST',
                 data: songIE,
                 datatype: 'json',
                 url: 'session.php',
                 async: true,
                 cache: false
             });
         });
     });
    

    thanks4all