正如標(biāo)題所說(shuō),我無(wú)法在我的 javascript 中獲取和使用數(shù)據(jù)庫(kù)中的數(shù)據(jù)。我有一個(gè)控制器,其中有我的數(shù)據(jù)庫(kù)上下文構(gòu)造函數(shù)和一個(gè) JsonResult 操作,其中信息按原樣出現(xiàn),但是當(dāng)我嘗試在 javascript 中使用數(shù)據(jù)時(shí),我相信它是空的。這是我的控制器:namespace ProiectColectiv.Controllers{ public class AdminMapController : Controller { private readonly ProiectColectivContext _context; public AdminMapController(ProiectColectivContext context) { _context = context; } public ActionResult AdminMap(User user) { return View("~/Views/Home/AdminMap.cshtml", user); } public JsonResult GetAllLocation() { var data = _context.Parkings.ToList(); return Json(data); } }}這是我的 .cshtml 和 javascript:@*@model ProiectColectiv.Models.Parking*@@{ ViewData["Title"] = "Admin Map";}<br/><!DOCTYPE html><html><head> <meta name="viewport" content="initial-scale=1.0"> <meta charset="utf-8"> <style> /* Always set the map height explicitly to define the size of the div * element that contains the map. */ #map { height: 800px; width: 1200px; } </style> </head><body><div id="map"></div><script> var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { center: { lat: 46.770920, lng: 23.589920}, zoom: 13 }); $.get("@Url.Action("GetAllLocation","AdminMap")", function (data, status) { var marker = []; var contentString = []; var infowindow = []; for (var i = 0; i < data.length; i++) { marker[i] = new google.maps.Marker({ position: { lat: parseFloat(data[i].Latitudine), lng: parseFloat(data[i].Longitudine) }, map: map });
2 回答

慕容3067478
TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
我認(rèn)為您需要JsonRequestBehavior.AllowGet
在方法中添加作為第二個(gè)參數(shù)Json()
。
return Json(data, JsonRequestBehavior.AllowGet)

阿晨1998
TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
解決。問(wèn)題是return Json(data)
返回一個(gè)字符串,類似的東西{name: "x"}
,我試圖把它當(dāng)作data.Name
我應(yīng)該做的data.name
,因?yàn)樗鼇?lái)自 json。
- 2 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報(bào)
0/150
提交
取消