为什么我不能用jquery将json字符串发送到php?
我试图存储以下数据,这些数据取自html为什么我不能用jquery将json字符串发送到php?,php,jquery,ajax,json,jplayer,Php,Jquery,Ajax,Json,Jplayer,我试图存储以下数据,这些数据取自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/M
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"
我的html代码是:
<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>
但它不起作用,有没有办法让它变得更好更干净?修复您的多个语法错误:
$(document).ready(function () {
$('.add-music').click(function () {
$.ajax({
type: 'POST',
data: function () {
var songNew = JSON.stringify({
title: $(this).attr('data-title'),
artist: $(this).attr('data-artist'),
mp3: $(this).attr('href')
});
return songNew;
},
datatype: 'json',
url: 'session.php',
async: true,
cache: false
});
});
});
也许最好是:
$(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')
});
$.ajax({
type: 'POST',
data: songNew,
datatype: 'json',
url: 'session.php',
async: true,
cache: false
});
});
});
数据的构造方式存在问题。使用类似以下内容:
'data': JSON.stringify({
title: $(this).attr('data-title'),
artist: $(this).attr('data-artist'),
mp3: $(this).attr('href'),
}),
这将在post正文中向服务器传递一个json编码的字符串。如果您希望将变量视为标准的post变量,请跳过json编码步骤。最后,结果很好
$(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
});
});
});
感谢4All请定义“不起作用”。您检查过请求头了吗?您检查过服务器得到什么了吗?
fuction
在JavaScript中不是有效的关键字。按照数据的构造方式,不会将任何内容传递回服务器。数据是一个函数吗?数据是要发送的数据,不是函数。@Curlas:不是,不是函数。显然,这是行不通的。这是一种功能。我也不知道那是什么。注意:对于显示的html中这些属性的内容,有些是空的,我不知道它们是如何填充的……但这是您应该使用的表单-或者使用.data()
函数perhaps@laur-由于您是SO的新手,欢迎您当您收到并回答您认为可以接受的问题时,正确的方法是现在单击该答案上的复选标记,以便系统将其显示为“已回答”,并且人们不会重新回答或寻求回答,从而节省他们回答新问题/其他问题的时间。很高兴这对你有用。
$(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
});
});
});