我需要从ASP.NET MVC的“ App_Data”文件夹中加载Geojson文件,并在d3.json中使用它。
function sizesToArray($string)
{
$sizesArray = explode(',',$string);
$returnArray = array();
foreach($sizesArray as $size)
{
$s = explode(':',$size);
// by using keyed array you are saving the FOREACH-loop
// for finding the relevant size
$returnArray[$s[0]] = $s[1];
}
return $returnArray;
}
// you can also use array_map() to return an array of "SIZE:QUANTITY" elements
// and then use implode(",") on it
function sizesToString($sizes)
{
$sizesString = '';
// $sizes is keyed by SIZE_NAME
foreach($sizes as $size_name => $size_qty)
{
$sizesString .= $size_name.':'.$size_qty.',';
}
$trimmed = rtrim($sizesString, ',');
return $trimmed;
}
$itemQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
$iresults = mysqli_fetch_assoc($itemQ);
$items = json_decode($iresults['items'],true);
foreach($items as $item)
{
$item_id = $item['id'];
$productQ = $db->query("SELECT sizes FROM products WHERE id = '{$item_id}'");
$product = mysqli_fetch_assoc($productQ);
$sizes = sizesToArray($product['sizes']);
$sizes[$item['size']] -= $item['quantity']; // we deduce the relevant q-ty
$sizesString = sizesToString($sizes);
$db->query("UPDATE products SET sizes = '{$sizesString}' WHERE id = '{$item_id}'");
}
我看到了错误:
d3.json("@Server.MapPath("~/App_Data/data/afile.geojson")", function (error, geodata) {
if (error) return console.log(error);
features.selectAll("path")
.data(geodata.features)
...
}
在“查看页面源”中:
error { target: XMLHttpRequest, isTrusted: true, lengthComputable: false, loaded: 0, total: 0, currentTarget: XMLHttpRequest, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, … }
如何解决这个问题?
答案 0 :(得分:0)
下面的代码应添加到Web.config(而不是web.config)中。
<system.webServer>
<staticContent>
<mimeMap fileExtension=".geojson" mimeType="application/geojson" />
</staticContent>
</system.webServer>
使用名为“数据”的文件夹中的文件。
d3.json("/Data/geo/afile.geojson", function (error, geodata) {
...}
使用“ App_Data”文件夹时看到错误。
d3.json("/App_Data/geo/afile.geojson", function (error, geodata) {
...}