DanielD DanielD - 4 months ago 14
Ajax Question

Rendering ViewComponent with ajax don't work in Edge/IE11

I have a simple ViewComponent works correctly in the browser chrome and Firefox but do not work in a browser Edge \ IE 11.

The component shows the time (just for example).

Chrome and firefox time is displayed.

Edge Browser \ IE 11, time is displayed only the first time the page is up and remains so, time is "frozen".

but

When I open the browser Edge development tools as well as in IE 11, the component starts to work properly and current time is displayed (just like other browsers)

And when I close the F12 development tools, component stops working (time "frozen").

And it repeated - F12 open the component works, F12 Close component stops working.

Am I missing something here ?

Attaching the simple code of the process
Thank you





**HomeController**

public IActionResult GetTime()
{
return ViewComponent("GetTime");
//// var time = DateTime.Now.ToString("h:mm:ss");
//// return Content($"The current time is {time}");
}


**GetTimeViewComponent**

public class GetTimeViewComponent : ViewComponent
{

public IViewComponentResult Invoke()
{

var model = new string[]
{
"Hello", DateTime.Now.ToString("h:mm:ss") , "the", "view", "component."
};
return View("Default", model);

}

}

**Default**

@model string[]
<ul>
@foreach (var item in Model)
{
<li class="text-danger">@item</li>
}
</ul>

**Index**

<div id="myComponentContainer">@await Component.InvokeAsync("GetTime") </div>

**Js / Ajax**

$(function () {

var refreshComponent = function () {
$.get("Home/GetTime", function (data) {
$("#myComponentContainer").html(data);
});
};
$(function () { window.setInterval(refreshComponent, 1000); });

});

Answer

Try to set cache false in ajaxSetup

$.ajaxSetup({ cache: false });

Or use

$.ajax({ cache: false, //other options... });
Comments