Return result JSON include HTML tag in ASP.NET MVC
I have system that send data in json format from ASP.NET MVC to android system.
I try sample from this site to achieve transfer data. When I try to call site to return result, there always return result without tag HTML in web browser but it work and display in web browser. In first time, I try in JQuery to display the result, but always say there no data. After that I test it Android system,开发者_如何学JAVA but result always return "{}" that mean no data there. Then I found there problem, about result Json from ASP.NET. Because I'm curious about this problem, I try sample JSON from this site, then it work in android.
My question why this could be happen and I could be resolve?
Sample Json work from site:
{"query":"Bo","suggestions":["Bognor
Regis","Bolton","Bournemouth","Camborne","Eastbourne","Loughborough",
"Peterborough","Scarborough","University of Bolton","Boston
University","Bournemouth University","Camborne School of Mines",
"Loughborough University","Ravensbourne College of Design and
Communication","University of Hull (Scarborough Campus)"]}
Sample Json Doesn't Work retrieve from ASP.NET MVC using JsonResult:
[{"Name":"Saab","Color":"Red"},{"Name":"Volvo","Color":"Blue"}]
Your question is not very clear but as far as I understand it you have an ASP.NET MVC site that is consumed by an Android client and you want this site to send JSON formatted data. If this is the case you could return a JsonResult from your controller action:
public ActionResult SomeAction()
{
// The data could be any class you would like to serialize
var data = new
{
query = "Boo",
suggestions = new[]
{
"Bognor Regis", "Bolton"
}
};
return Json(data, JsonRequestBehavior.AllowGet);
}
精彩评论