Quote:
Hi guys i am having issue such that when i did not do anything on the dashboard for a specific period of time while the ajax is retrieving the data from the database . it would throw me an error saying that localhost is undefined. Here is the code in doing the ajax
JavaScript Code
var marker1 = [];
var circle = [];
L.Circle.include({
contains: function (latLng)
{
return this.getLatLng().distanceTo(latLng) < this.getRadius();
}
});
$.ajax({
type: "GET",
url: 'http://localhost:59927//api//Values//FlagingDevice(WithoutParameters)',
success: function (data, status, xhr)
{
for (var i = 0; i < data.Table.length; i++)
{
circle[i] = L.circle([data.Table[i].Latitude, data.Table[i].Longitude], 50, { color: '#DA2E2E', opacity: 2, fillColor: 'blue', fillOpacity: .3 }).addTo(map);
}
},
error: function (xhr)
{
alert(xhr.responseText);
}
});
function innerOne()
{
$.ajax({
type: "GET",
url: 'http://localhost:59927//api//Values//FlagingDevice(WithoutParameters)',
success: function (data, status, xhr)
{
for (var s = 0; s < marker1.length; s++)
{
map.removeLayer(marker1[s]);
}
for (var i = 0; i < data.Table.length; i++)
{
var value = i + 1;
if (circle[i].contains(L.latLng([data.Table[i].Latitude, data.Table[i].Longitude])))
{
var customPopup1 = 'Station: ' + data.Table[i].StationName;
var customOptions1 =
{
'maxWidth': '500',
'className': 'custom'
};
circle[i].bindPopup(customPopup1, customOptions1);
setTimeout(function () { innerOne(); }, 30000);
}
else
{
marker1[i] = L.marker([data.Table[i].Latitude, data.Table[i].Longitude]).addTo(map);
var customPopup = 'Latitude: ' + data.Table[i].Latitude + '</br>Longitude: ' + data.Table[i].Longitude
+ '</br>Station: ' + data.Table[i].StationName + ' </br>Box: ' + data.Table[i].Name + '</br>Timestamp: ' + data.Table[i].LocationSend + `<br/><a target='_blank' href='/Home/History?DeviceID=${value}' style='color: #000000'>Click Here For Location History</a><br/>`;
marker1[i].bindPopup(customPopup);
setTimeout(function () { innerOne(); }, 30000);
}
}
},
error: function (xhr)
{
alert(xhr.responseText);
}
});
}
Quote:
ValuesController.cs
[HttpGet]
[ScriptMethod(UseHttpGet = true)]
[Route("api/Values/FlagingDevice(WithoutParameters)")]
public DataSet FlagingDevice1()
{
DataSet ds = dblayer.FlagingDeviceWithoutParameters();
return ds;
}
}
}
Quote:
db.cs
public DataSet FlagingDeviceWithoutParameters()
{
SqlCommand com = new SqlCommand("FlagingDevice(Without Parameters)", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
Quote:
The Response
<imei>351246879634268</imei>
<DeviceImei>351246879634268</DeviceImei>
<Latitude>1.321602 </Latitude>
<Longitude>103.860449</Longitude>
<Distance>0</Distance>
<LocationSend>2018-10-26T12:00:00+08:00</LocationSend>
<StationName>Maha Bodhi School</StationName>
<DivisionName>Ang Mo Kio GRC</DivisionName>
<GAROName>Amanda Sam</GAROName>
<BoxName>8</BoxName>
What I have tried:
I have tried to switch the interval to timeout as interval create a bunch of ajax request while timeout only create once after a specific period of time.