My attempt to call a C# MVC controller method from a custom JavaScript script using Ajax seems to be encountering an issue with accepting array entries as arguments within the Ajax request.
I tested assigning them to non-array variables, which worked, but I specifically need to pass them as array elements.
public async Task<ActionResult> CallEngineAsync(int id, string command)
{
string path = Server.MapPath("/PluginDLLs/");
string output = await EngineBroker.CallEngine(command, path, AppDomain.CurrentDomain);
// Session["output"] = output;
// Session["calledPlugin"] = id;
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home");
return Json(new { Url = redirectUrl });
}
var pluginIDs = [];
var pluginCommands = [];
var pluginIntervals = [];
$(".p-container").find(".p").each(function ()
{
var confi = $(this).find("#command").attr("value");
var interval = 1000 * confi.replace(/\D/g, '');
if (confi.includes("interval") && interval > 0)
{
var id = $(this).attr("data-pluginid");
pluginIDs.push(id);
pluginCommands.push(confi);
pluginIntervals.push(interval);
}
});
for (let p = 0; p < pluginIDs.length; p++)
{
window.setInterval(function ()
{
$.ajax(
{
url: 'Home/CallEngineAsync',
data: { id: pluginIDs[p], command: pluginCommands[p] },
traditional: true,
type: 'POST',
success: function (data)
{
window.location.href = data.Url;
},
error: function (xhr, status, error) { }
});
}, pluginIntervals[p]);
}
The error message received states: POST https://localhost:44381/Home/CallEngineAsync 500 (Internal Server Error)