简体   繁体   中英

JSON returning object Object not String

The below code is returning [object Object] not a string. How can I get it to return the correct string representation? I am using ASP.NET MVC4 C#.

public JsonResult Names(string name)
{
    var a = db.NamesToGet.Select(e => new 
    {
        name = e.Names                        
    });          
    return Json(a, JsonRequestBehavior.AllowGet);
}

尝试

var a = db.NamesToGet.Select(e=>e.Names);

Access it with data.name or substitute data to your json variable name.

$.get("/Controller/GetName", function(data) {
   alert(data.name);
   //or alert(data[0].name);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM