【问题标题】:webmethod asp.net status of 500 (Internal Server Error)webmethod asp.net 状态为 500(内部服务器错误)
【发布时间】:2016-03-01 22:24:28
【问题描述】:

我需要帮助。 我在 asp.net 中做一个 webmethod,但我总是收到消息“加载资源失败:服务器响应状态为 500(内部服务器错误)”

在我的服务器中我有这段代码(抱歉我的代码质量很差,但这是第一次在 c# 和 asp.net 中使用)

`

public class nLinha
        {
            public string tipo { get; set; }
            public string dataInicial { get; set; }
            public string dataFinal { get; set; }
            public string no { get; set; }
        }

        public class nLinhaResposta
        {
            public string stamp { get; set; }
            public string documento { get; set; }
            public string numero { get; set; }
            public string nData { get; set; }
            public string referencia { get; set; }
            public string designacao { get; set; }
            public string quantidade { get; set; }
            public string vtotal { get; set; }
            public string vunitario { get; set; }
        }

        [WebMethod]

        public static string desenhaTabela(nLinha linha)
        {
            List<nLinhaResposta> resposta = new List<nLinhaResposta>();
            //TDS ou de uma REF ?
            if (linha.tipo != "REF")
            {
                GridView grdDados = new GridView();
                grdDados.DataSource = MyCliente.GetConsumos(linha.no, Convert.ToDateTime(linha.dataInicial), Convert.ToDateTime(linha.dataFinal));
                grdDados.DataBind();

                foreach (GridViewRow row in grdDados.Rows)
                {
                    nLinhaResposta x = new nLinhaResposta();
                    x.stamp = row.Cells[0].Text.ToString().Trim();
                    x.documento = row.Cells[5].Text.ToString().Trim();
                    x.numero = row.Cells[6].Text.ToString().Trim();
                    x.nData = row.Cells[4].Text.ToString().Trim();
                    x.referencia = row.Cells[1].Text.ToString().Trim();
                    x.designacao = row.Cells[2].Text.ToString().Trim();
                    x.quantidade = row.Cells[3].Text.ToString().Trim();
                    x.vtotal = row.Cells[7].Text.ToString().Trim();
                    x.vunitario = row.Cells[8].Text.ToString().Trim();
                    resposta.Add(x);

                }
            }
            else
            {
                GridView grdDados = new GridView();
                grdDados.DataSource = MyCliente.GetConsumos(linha.no, linha.tipo, Convert.ToDateTime(linha.dataInicial), Convert.ToDateTime(linha.dataFinal));
                grdDados.DataBind();

                foreach (GridViewRow row in grdDados.Rows)
                {
                    nLinhaResposta x = new nLinhaResposta();
                    x.stamp = row.Cells[0].Text.ToString().Trim();
                    x.documento = row.Cells[5].Text.ToString().Trim();
                    x.numero = row.Cells[6].Text.ToString().Trim();
                    x.nData = row.Cells[4].Text.ToString().Trim();
                    x.referencia = row.Cells[1].Text.ToString().Trim();
                    x.designacao = row.Cells[2].Text.ToString().Trim();
                    x.quantidade = row.Cells[3].Text.ToString().Trim();
                    x.vtotal = row.Cells[7].Text.ToString().Trim();
                    x.vunitario = row.Cells[8].Text.ToString().Trim();
                    resposta.Add(x);

                }
            }
            JavaScriptSerializer resultado = new JavaScriptSerializer();
            return resultado.Serialize(resposta);

        }

`
and in the client i have this

var obj = {};
obj.tipo = $("#tipo option:selected").val();
var from = $("#dataI").val().split("/");
var dataI = from[2]+"-"+from[0]+"-"+from[1];

var from1 = $("#dataF").val().split("-");
var dataF = from1[2]+"-"+from1[0]+"-"+from1[1];

obj.dataInicial = dataI.toString();
obj.dataFinal = dataF.toString();
obj.no = $("#cliente option:selected").val();

$.ajax(
 {
  url: "verConsumos.aspx/desenhaTabela",
 contentType: 'application/json; charset=utf-8',
  dataType: 'json',
   data: JSON.stringify({ linha: obj }),
    type: "POST",
    success: function (data) {
   console.log(data);
   toastr.success("Sucesso ", "Sucesso", opts);
    },
    erro: function (XMLHttpRequest, textStatus, errorThrown) {
     toastr.error("Erro com a operação", "Erro", opts);
     }
  });

【问题讨论】:

  • 不要添加不相关或错误的标签!
  • 我没听懂你的回答

标签: jquery asp.net json


【解决方案1】:
public ActionResult desenhaTabela(nLinha linha)
{
    try 
    {
      ....logic here....
      return Json(data, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        //TODO: log exception
        return new HttpStatusCodeResult(401, ex.Message);
    }
}

你也可以这样返回:

return Content(jsonObject.ToString(), "application/json");

return Content("Your message",...

然后在您的 ajax 调用中将成功更改为:

 $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "/someDir/someAjaxControllerMethod",
        data: jStr,
        success: function (json) {
            ...do something...
            var s = JSON.stringify(json);
            alert(s);
        },
        error: function (event) {
            alert(event.statusText);
        }
    });

【讨论】:

  • 我使用的是 webform 而不是 mvc
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多