【发布时间】:2018-10-30 22:17:40
【问题描述】:
在执行 Ajax get 请求以从 mvc asp.net 的另一个视图获取图像时,出现以下控制台错误,函数未命中成功方法:
"GET http://localhost:1879/cardimages/getimage?id=12 500 (Internal Server Error)"
下面我添加了控制器的所有三个代码 sn-ps、ajax 调用和下拉视图代码,我试图通过这些代码从 db 中获取图像
我正在尝试从 asp.net mvc 的下拉选择中获取图像:
以下是 AJAX 代码:
$(function () {
$("#ImageId").change(function (evt) {
var DropDownSelectedVal = $("#ImageId :selected").val();
alert(DropDownSelectedVal);
if (DropDownSelectedVal != null) {
alert("Success");
$.ajax({
url: "@Url.Action("GetImage", "CardImages")",
type: "Get",
dataType: 'json',
contentType: "Image",
data: {id: DropDownSelectedVal},
success: function (data) {
alert("Success");
$("img-upload2").attr('src', data);
},
error: function (xhr) {
alert("Something went wrong, please try again");
}
});
}
});
});
以下是控制器中的功能:
public ActionResult GetImage(int id /* drop down value */)
{
var cardImage = db.CardImages.Where(x=>x.CityId == id ).FirstOrDefault();
string dir = ConfigurationManager.AppSettings["UploadFilesPath1"].ToString() + "/";
string logoPath = GetFileFromFolder(dir, cardImage.CardFileName + cardImage.OriginalFileName);
if (logoPath != string.Empty)
{
Byte[] bytes = System.IO.File.ReadAllBytes(logoPath);
cardImage.LogoImage = "data:image/jpeg;charset=utf-8;base64," + Convert.ToBase64String(bytes);
}
//var model = db.CardImages.Find(id); // This is for example put your code to fetch record.
//ViewBag.img = (from s in db.CardImages where s.CityId == id select s.CardFileName + s.OriginalFileName);
return Json(cardImage.LogoImage);
}
我正在使用这个下拉列表
@Html.DropDownList("CityId", null, "Select City", htmlAttributes: new { @class = "form-control", @Id = "ImageId" })
我在警告中收到浏览器错误,因为出现问题和控制台错误,如上所述。
【问题讨论】:
-
500(内部服务器错误) - 这意味着错误在服务器端,而不是在您的 jquery 客户端。
-
尝试调试你的服务器代码并找到问题
-
你能详细说明@Rajesh
-
打开浏览器控制台并切换到网络选项卡并进行 ajax 调用。在列表中,您现在应该会看到带有红色文本的文件。单击它,它将显示来自服务器的响应的预览。这将帮助您尝试缩小问题范围
标签: jquery html asp.net ajax web