I am trying to access a global javascript variable in order to pass it as part of the data to my ajax function. Struggling with how to do it because imageIndex does not exist in the current context.
Javascript:
<script type="text/javascript">
var imageIndex = 0;
$(document).ready(function () {
var imageIndex = 0;
getImage();
function getImage() {
$.ajax({
type: "GET",
url: '@Url.Action( "GetImage", "Tally" )',
data: { imageName: '@(ViewBag.images[imageIndex])', contractID: '@(ViewBag.contractId)' },
//dataType: "image/jpeg;base64",
success: function (data) {
console.log(data);
$('#scanImage').attr('src', 'data:image/jpeg;base64,' + data.myImage);
$("#imageName").val('@(ViewBag.image)');
imageIndex++;
},
error: function () {
console.log("got error");
}
});
}
});
</script>
imageIndexvariables in your code, and actually both of them are in scope in your$.ajaxcall?@(…)syntax? I suspect you're mixing up client- and serverside processing here.