Okey问题是我进行了ajax调用,当任何属性为null时,ajax都可以正常工作,但是当所有属性okey ajax都说“ 404 not found”时,有一个有趣的事情,该问题仅在服务器上发生。在我的本地主机上没问题。
$(".add-button").click(function (e) {
e.preventDefault();
var id = "#";
var langId = $(".select-language").val();
id += langId;
var realpartno = $(id+" .input-tags").val();
var value = [];
var newRealPart ="";
for (var i = 0; i < realpartno.length; i++) {
if (realpartno[i] != ",") {
newRealPart += realpartno[i];
}
else {
value.push(newRealPart);
newRealPart = "";
}
if (i == realpartno.length - 1) {
value.push(newRealPart);
}
}
var formData = new FormData();
formData.append("Description", $(id + " .description").val());
formData.append("SubCategory", $(id + " .subcategory").val());
formData.append("RealPartNos", value);
formData.append("Categories", $(id +" .category").val());
formData.append("Photo", $(id + " .product_photo").get(0).files[0]);
formData.append("LanguageId", $(".select-language").val());
$(id + " .error_view").empty();
$(id + " .error_view").text("Zehmet Olmasa Gozleyin Sorgu Icra Edilir ...")
$.ajax({
url: "/Admin/Home/Add/",
type: "post",
dataType: "json",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (response) {
if (response.status == 200) {
swal({
title:"Success",
icon: "success",
text: response.message,
buttons: ["Siyahiya Kec", "Yeni Mehsul Elave Et"]
});
ClearArea("#description");
ClearArea("#product_photo");
ClearElement(".input-tags .item");
var a = document.createElement("a");
a.href = "/Admin/Home/Index";
a.innerText = "Siyahiya Qayit";
a.className = "btn btn-success my-2";
a.style.color = "#fff";
$(id + " .error_view").empty();
$(id + " .error_view").append(a);
} else {
swal({
title: "Error",
icon: "error",
text: response.errorMessage
});
}
}
});
})
为什么我的本地主机没有任何问题,只有一切正常时服务器上才有问题。这就是我的行为
[HttpPost]
public async Task<JsonResult> Add(ProductViewModel model)
{
//Check All Models
foreach (string item in model.Categories)
{
if (item == null)
{
return Json(new { status = 400, errorMessage = "En azi 1 category elave edin" });
}
}
if (model.Description == "")
{
return Json(new { status = 400, errorMessage = "Description elave edin" });
}
if (model.SubCategory == "")
{
return Json(new { status = 400, errorMessage = "En azi 1 Subcategory elave edin" });
}
if (model.Photo == null)
{
return Json(new { status = 400, errorMessage = "Sekil elave edin" });
}
foreach (string item in model.RealPartNos)
{
if (item == null)
{
return Json(new { status = 400, errorMessage = "En azi 1 realpartno elave edin" });
}
}
if (model.LanguageId == "")
{
return Json(new { status = 400, errorMessage = "Dil elave edile bilmir" });
}
//Find Language
Language language = db.Languages.Where(l => l.Key == model.LanguageId).FirstOrDefault();
var sub = model.SubCategory;
var description = model.Description;
//Make Category and RealPartNo Arrays
List<string> categories = MakeArray(model.Categories);
List<string> realPartNos = MakeArray(model.RealPartNos);
//Upload Photo
var photo = model.Photo;
var file_name = photo.FileName;
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads", "Products", file_name);
if (MyFile.isImage(photo))
{
await MyFile.Upload(photo, path);
}
//Create Product
Product product = new Product();
product.SubCategory = await db.SubCategories.Where(s => s.Name == sub).FirstOrDefaultAsync();
product.PhotoPath = photo.FileName;
//Creat RealPartNo
foreach (var realPartNo in realPartNos)
{
RealPartNo number = new RealPartNo();
number.Name = realPartNo;
number.Product = product;
await db.RealPartNos.AddAsync(number);
}
//Create Category
foreach (var item in categories)
{
if (item != null)
{
Category category = await db.Categories.Where(c => c.Name == item).FirstOrDefaultAsync();
ProductCategory productCategory = new ProductCategory();
productCategory.Category = category;
productCategory.Product = product;
db.ProductCategories.Add(productCategory);
}
}
//Create MultiLanguage Product
ProductLanguage productLanguage = new ProductLanguage();
productLanguage.LanguageId = language.Id;
productLanguage.Product = product;
productLanguage.Description = model.Description;
await db.ProductLanguages.AddAsync(productLanguage);
await db.Products.AddAsync(product);
await db.SaveChangesAsync();
return Json(new { status = 200, message = "Mehsul Elave Edildi" });
}