I need to place the result of an ajax get into a javascript variable.
The following works
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
$("#divEdit").html(html);
});
This does not work
var editHtml = "";
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
editHtml= html;
});
$("#divEdit").html(editHtml);
Have also tried
var editHtml = "";
editHtml = $.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
return html;
});
$("#divEdit").html(editHtml);
How can I get it to work?
I’ve never tried using
@Url.Actioninside an$.ajaxcall (so I’m not 100% sure it works), but you could try using it since it gives you a more granular approach to ajax requests. In thesuccesscallback, you could$.ajaxoptions even accept a parameter namedasyncwhich you can set to false per your comment in @aroth’s answer.