【问题标题】:Query SQL and assign it to javascript查询 SQL 并将其分配给 javascript
【发布时间】:2013-10-22 16:54:46
【问题描述】:

我有一个 .js 文件:

    $(function(){
                      var playItem = 0,
                  title=$('.jp-interface .jp-title'),
                  jPlayer=$("#jplayer"),
                  myPlayList = [
            {name:"BlackPlant",mp3:"audio/black_plant.mp3",ogg:"audio/black_plant.ogg"},
            {name:"Hidden",mp3:"audio/hidden.mp3",ogg:"audio/hidden.ogg"},
            {name:"The Separation",mp3:"audio/separation.mp3",ogg:"audio/separation.ogg"}
        ],      
        jPlay=function(idx){
            if(typeof idx==typeof 0)
                jPlayer.jPlayer("setMedia",myPlayList[idx]).jPlayer('play')
            if(typeof idx==typeof '')
                jPlayer.jPlayer("setMedia",myPlayList[playItem=idx=='next'?(++playItem<myPlayList.length?playItem:0):(--playItem>=0?playItem:myPlayList.length-1)]).jPlayer('play')                 
            title.text(myPlayList[playItem].name)
            Cufon.refresh()
        }

    jPlayer.jPlayer({
        ready: function() {
            jPlay(playItem)
        },
        ended:function(){
            jPlay('next')
        }
    })

    $(".jp-prev,.jp-next")
        .click( function() { 
            jPlay($(this).is('.jp-next')?'next':'prev')
            return false;
        })

});

--> 数据库存储在 sql server 中:

  • name (nvarchar)
  • path (nvarchar)

我的音乐播放器在.aspx 页面中使用这个脚本来播放音乐。我想在 sql server 中查询路径和名称并将它们分配给 myPlayList .js 文件。有没有办法做到这一点?

【问题讨论】:

    标签: jquery asp.net sql


    【解决方案1】:

    在您的 Aspx 页面中添加此脚本

    <script type="text/javascript">
    function QueryDB() {
        PageMethods.GetServerData(OnSucceeded, OnFailed);
    }
    
    function OnSucceeded(result, userContext, methodName)  {
        var name= result[0];//You will get your name here
        var path = result[1];//and your path here
    }
    
    function OnFailed(error, userContext, methodName) {
       alert(error);
    }
    </script>
    

    您还必须添加

    <asp:ScriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true" EnablePageMethods="true" />
    

    在您的 Aspx 页面中

    现在在你后面的代码中你必须添加一个这样的页面方法

    [System.Web.Services.WebMethod]
    public static string[] QueryDB()
    {
    /*   Query your database from here and pass the name and path where I am passing
     myName and myPath*/
        return new string[] { myName , myPath};
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      相关资源
      最近更新 更多