Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法为jquery中的函数提供url_Javascript_Jquery_Json_Api_Jplayer - Fatal编程技术网

Javascript 无法为jquery中的函数提供url

Javascript 无法为jquery中的函数提供url,javascript,jquery,json,api,jplayer,Javascript,Jquery,Json,Api,Jplayer,变量url=”http://localhost:8000/api/songs/rock"; 吴松; 艺术家; var体裁; var相册; $.ajax({ url:url, 键入:“GET”, 数据类型:“json”,//添加了数据类型 成功:功能(res){ 控制台日志(res); 宋=res.song; 艺术家=res.artist.name; 相册=res.album.albumb_title; 流派=res.genre.name; console.log(歌曲); 控制台日志(艺术家);


变量url=”http://localhost:8000/api/songs/rock";
吴松;
艺术家;
var体裁;
var相册;
$.ajax({
url:url,
键入:“GET”,
数据类型:“json”,//添加了数据类型
成功:功能(res){
控制台日志(res);
宋=res.song;
艺术家=res.artist.name;
相册=res.album.albumb_title;
流派=res.genre.name;
console.log(歌曲);
控制台日志(艺术家);
console.log(体裁);
控制台日志(相册);
}
});
$(文档).ready(函数(){
$(“#jquery_jplayer_1”).jplayer({
就绪:函数(){
$(this).jPlayer(“setMedia”{
mp3:歌曲,
});
},
swfPath:“http://jplayer.org/latest/js",
提供:“mp3”,
});

});,因为您正在使用
歌曲
,然后将其初始化为响应中获得的值

只要多读一点关于asyn的内容

一个快速的解决方案是将您的歌曲播放脚本包装在一个函数中,然后在ajax请求的成功回调中调用该函数


变量url=”http://localhost:8000/api/songs/rock";
吴松;
艺术家;
var体裁;
var相册;
$.ajax({
url:url,
键入:“GET”,
数据类型:“json”,//添加了数据类型
成功:功能(res){
控制台日志(res);
宋=res.song;
艺术家=res.artist.name;
相册=res.album.albumb_title;
流派=res.genre.name;
console.log(歌曲);
控制台日志(艺术家);
console.log(体裁);
控制台日志(相册);
播放歌曲(歌曲)
}
});
功能播放歌曲(歌曲){
$(“#jquery_jplayer_1”).jplayer({
就绪:函数(){
$(this).jPlayer(“setMedia”{
mp3:歌曲,
});
},
swfPath:“http://jplayer.org/latest/js",
提供:“mp3”,
});
}

在ajax就绪后放置jplayer函数调用

$(document).ready(function(){
     $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json', // added data type
            success: function(res) {
                 $("#jquery_jplayer_1").jPlayer({
                 ..........
            }
        });
为了举例说明,我跳过了一些代码


首先,当文档准备就绪时调用ajax,当ajax完成时调用jPlayer函数。

据我所知,您需要将jPlayer代码放入函数中,并在ajax调用函数的响应中调用它,然后将歌曲作为参数传递给该函数。。。