English 中文(简体)
通过ajax asp.net 问题接收json
原标题:receiving json via ajax asp.net issue

I am trying to receive a json data using ajax asp.net. i have got a web service with a web method -

[WebMethod]
public List<Song> GetSongListByMood(string Mood)
{
    SongBL songbl = new SongBL();
    return songbl.GetSongListByMoodBL(Mood);
}

我拿到了刺杀密码...

        $(document).ready(function () {
        var cssSelector = {
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        };
        var playlist = [];
        var options = {
            swfPath: "./js",
            supplied: "mp3"
        };
        var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);
        $("#slider a").click(function () {
            var mood = $(this).text();
            var xhr = new XMLHttpRequest();
            var url = "AvironaService.asmx/GetSongListByMood";
            xhr.open("POST", url, true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var obj = JSON.parse(xhr.responseXML.text);
                    myPlaylist.playlist = obj;
                }
            };
            var contentType = "application/x-www-form-urlencoded"
            xhr.setRequestHeader("Content-Type", contentType);
            var qs =  Mood=  + mood;
            xhr.send(qs);
        });});

现在我想做的基本上就是用json格式的ajax格式从服务器上获取数据,并将数据放入播放列表变量

问题回答

您需要做一些修改 。

  1. 更改您返回 string 的方法, 而不是 List< Song>

  2. 添加语句 使用系统。 Web.Script.Servication

  3. 创建 JavaScript序列化器 实例, 并使用此实例对对象进行序列化, 并返回结果 。

那么... So... So...

using System.Web.Script.Serialization;

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]     
public string GetSongListByMood(string Mood)
{
    SongBL songbl = new SongBL();
    var jss = new JavaScriptSerializer();
    return jss.Serialize(songbl.GetSongListByMoodBL(Mood));
}

更改 AJAX 代码, 以利用可用的 JQuery 方法 :

$("#slider a").click(function () {
    $.ajax({
        "url" : "AvironaService.asmx/GetSongListByMood",
        "type" : "post",
        "data" : {"Mood" : $(this).text()},
        "dataType" : "json"
        "success" : function(data){
          myPlaylist.playlist = data;
        }
    });
});




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签