Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

javascript - 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);
}

and i have got the javascript code -

        $(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);
        });});

now basically what im trying to do is get the data from the server using ajax in json format and put the data in the playlist variable

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make a few changes.

  1. Change your method to return a string instead of List<Song>.

  2. add a using statement using System.Web.Script.Serialization.

  3. Create an instance of JavaScriptSerializer and use that to serialize your object and return the results.

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));
}

Change your AJAX code to leverage the JQuery methods available:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...