0

I make Get request to service from javascript to 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;
}

Title is undefined and t is

{"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"}

My c# model class:

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

And API method:

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 method:

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

How I can get valid json on js side? Or I need change type like string array?

SmiLe
  • 311
  • 2
  • 13
  • What is `typeof(t)`? – Phylogenesis Sep 01 '17 at 07:42
  • @Phylogenesis its string..... – SmiLe Sep 01 '17 at 07:42
  • 1
    Somehow your JSON has been doubly stringified then. That is, `JSON.parse(JSON.parse(service))` will return the original object. – Phylogenesis Sep 01 '17 at 07:43
  • 4
    If your using MVC you should be returning a `JsonResult` (not a `Task`) then `return Json(result);` – Liam Sep 01 '17 at 07:43
  • @Liam I need call await in this method, so I need return Task – SmiLe Sep 01 '17 at 07:48
  • 1
    *"false for synchronous request"* never, ever do this...Read [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Sep 01 '17 at 07:48
  • Ok well `Task` – Liam Sep 01 '17 at 07:49
  • 1
    @Liam `Task>` its work. Could you write like an answer – SmiLe Sep 01 '17 at 07:50
  • 1
    It's fine @SmiLe, this is a duplicate – Liam Sep 01 '17 at 07:50
  • 1
    @What I should to do for close this question? – SmiLe Sep 01 '17 at 07:52
  • You don't have enough rep to close this yourself (I take it you don't see a yellow bar?), just leave it. It'll get closed in due course. More infor here [What is a “closed”, “on hold”, or “duplicate” question?](https://meta.stackexchange.com/questions/10582/what-is-a-closed-on-hold-or-duplicate-question) and [How should duplicate questions be handled?](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled) – Liam Sep 01 '17 at 07:55
  • @Liam, ok thank you for help. Have a nice day! – SmiLe Sep 01 '17 at 07:56

0 Answers0