簡體   English   中英

找不到圖片AJAX(ASP.NET MVC)

[英]Image cannot be found AJAX(ASP.NET MVC)

我有AJAX電話

這是代碼

<script>
$(document).ready(function () {
    email_update();
});

function email_update() {
    $.ajax({
        url: '@Url.Action("EmailsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
            var email = result;
            // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++) {
                var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
                    '<b style="margin-left: 10px;">' +(i + 1) +
                    '<b style="margin-left:20px;">' + result[i].Email + '</b>'+
                    '<b>' +
                    '<b style="margin-left: 20px;">' +
                    result[i].Name +
                    '</b>' + '<a style="float: right; margin-right: 20px;">' +
                    '<img src="~/Images/Edit.png">' +
                    '</a>' +
                    '<a style="float: right; margin-right: 20px;">' +
                    '<img src="~/Images/Delete.png">' +
                    '</a>' +
                    '</div>';
                $(".email_list").append(emailHTML);
            }
        }
    });
}

但是我面臨麻煩

加載資源失敗:服務器響應狀態為404(未找到)

但是圖像在文件夾中

我以前有此代碼(不在AJAX中) <a style="float: right; margin-right: 20px;" href='@Url.Action("Edit_mail", "Questions", new {id = item.Individ_Id})'> <img src='@Url.Content("~/Images/Edit.png")'/> </a> <a style="float: right; margin-right: 20px;" href='@Url.Action("Edit_mail", "Questions", new {id = item.Individ_Id})'> <img src='@Url.Content("~/Images/Edit.png")'/> </a>

我的錯誤在哪里?

其原因是~在JavaScript / HTML中沒有意義。 在Razor中, @Url.Content()將虛擬路徑轉換為絕對路徑。

根據您的示例代碼使用它,或將引用存儲在變量中,以后再使用

var editImage = '@Url.Content("~/Images/Edit.png")'

用法

'<img src="' + editImage + '">'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM