Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 30-Jan-19 23:14pm
v4
Comments
Prilvesh K 22-Nov-18 22:52pm    
This is how I normally check :

You can debug by checking if your server is running and is serving on a correct port of 59927 by visiting this link directly from your web browser http://localhost:59927

Next check if you are doing a get request to the correct api route
http://localhost:59927//api//Values//FlagingDevice

Than check what is the raw response it give you and if you are parsing it correctly .

Most likely its one of the above issues .
lee loong 22-Nov-18 23:13pm    
I have checked the issues that you have stated and everything is correct. You may also check the controller in using the get request to retrieve the data from the database

1 solution

You definitely don't need double slashes in your ajax call
Use
http://localhost:59927/api/Values/FlagingDevice(WithoutParameters)
Instead of
http://localhost:59927//api//Values//FlagingDevice(WithoutParameters)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900