无法从服务器获取Json

时间:2017-09-01 07:39:20

标签: javascript c# json asp.net-mvc api

我发出请求从javascript到c#的服务

let service = httpGet("Rap god");
let t = JSON.parse(service);
            console.log("t",t);
            console.log("title",t.Title);

function httpGet(query)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", MSS+"/"+query, false ); // false for synchronous request
xmlHttp.send(query);
return xmlHttp.responseText;
}

标题未定义且t为

{"Title":"Rap God","HeaderImageUrl":"https://images.genius.com/5b600decc7e5a50cee55b42574c46783.1000x1000x1.jpg","Url":"https://genius.com/Eminem-rap-god-lyrics","TypAlbumName":"The Marshall Mathers LP2","PrimaryArtistName":"Eminem"}

我的c#模特课:

public string Title { get; set; }
public string HeaderImageUrl { get; set; }
public string Url { get; set; }
public string TypAlbumName { get; set; }
public string PrimaryArtistName { get; set; }

和API方法:

public async Task<string> Get(string id)
{
var result = new Data
{
   Title = hits[0].Result.Title,
   HeaderImageUrl = hits[0].Result.HeaderImageUrl,
   Url = hits[0].Result.Url,
   TypAlbumName = hits[0].Result.Album.Name,
   PrimaryArtistName = hits[0].Result.PrimaryArtist.Name
};
 return result.ToJSON();
}

ToJson方法:

 public static string ToJSON(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

如何在js端获得有效的json?或者我需要更改类型,如字符串数组?

0 个答案:

没有答案