Ajax响应在服务器端(iis)

时间:2017-12-21 14:25:40

标签: c# jquery ajax asp.net-mvc

我尝试使用以下函数js

在C#MVC和JS上保存文件(图像)
      function Add() {
            var data = new FormData();
            var files = $("#txtUploadFile").get(0).files;
            var cod = document.getElementById('cod').value;
            var mat = document.getElementById('mat').value;
            var status = document.getElementById('status').value;
            var plant = document.getElementById('plant').value;
            if (files.length > 0) {
                if (window.FormData !== undefined) {
                    var data = new FormData();
                    for (var x = 0; x < files.length; x++) {
                        data.append("file" + x, files[x]);
                        data.append("mat", mat);
                        data.append("status", status);
                        data.append("plant", plant);
                        data.append("code", cod);
                    }
                    $.ajax({
                        type: 'POST',
                        url: '/Pred/Admin/AddPred',
                        contentType: false,
                        processData: false,
                        data: data,
                        success: function (response) {
                                if(response.msg == 1)
                                {
                                    refreshTable(response.data);
                                }
                                alert('Predio agregado.');

                        },
                        error: function (xhr, status, p3, p4) {
                            var err = "Error " + " " + status + " " + p3 + " " + p4;
                            if (xhr.responseText && xhr.responseText[0] == "{")
                                err = JSON.parse(xhr.responseText).Message;
                        }
                    });
                }
            }
        }

在我使用它的代码隐藏

public ActionResult AddPred()
        {
            int isInsert=0;
            string route = ConfigurationManager.AppSettings["MAPS_ROUTE"];
            string[] status, plants, mats, codes;
            int stat;
            try
            {
                var requeststatus = Request.Params;
                status = requeststatus.GetValues("status");
                plants = requeststatus.GetValues("plant");
                codes = requeststatus.GetValues("cod");
                mats = requeststatus.GetValues("mat");
                if (status[0] == "On")
                    stat= 1;
                else
                    stat= 0;
                string plant = plants[0];
                string mat = mats[0];
                string code = codes[0];
                foreach (string file in Request.Files)
                {
                    var fileContent = Request.Files[file];
                    if (fileContent != null && fileContent.ContentLength > 0)
                    {
                        var fileName = fileContent.FileName;
                        var path = Path.Combine(Server.MapPath(route), fileName);
                        path = Server.MapPath(route) + fileName;
                        var sqlpath = "." + route + "/" + fileName;
                        fileContent.SaveAs(path);
                        isInsert = ma.InsertPred(code, mat, stat, plant, sqlpath);                        
                    }
                }
                merge.preds = ma.GetPreds();
                return Json(new { success = true, data = merge.preds, msg = isInsert }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("add failed");
            }

        }

但服务器响应是

  

POST myserver / Preds / Admin / AddPred 500(内部服务器错误)

我使用了console.log,但我无法获取错误信息,在本地使用此代码时,它运行Okey,保存图像并返回模型以刷新前端,但是当将应用程序放在服务器,只返回错误,其他功能工作(模态显示,返回模型与json)但不起作用保存图像,我在服务器文件夹上设置权限(写,加载,修改),

有人提出解决这个问题的想法,我不知道什么是错的

1 个答案:

答案 0 :(得分:0)

试试这样:

 function getCities(id) {
    $.ajax({
        type: "POST",
        url: "@Url.Action("Index", "Default")",
        data: '{id: id }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        //alert(response.responseData);
         window.location = '/Default/YourView';//

    },
    error: function (response) {
        alert("error!");  //
    }
    });
}


 //In your controller  
public JsonResult Index(string id)
{
    merge.country= mp.Country();
    merge.city= mp.City(id);

    return Json("you can return an object or a string, what you want");
}