2016-03-01 103 views
-1

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

我的服务器我有这个代码(我的代码遗憾的质量较差,但it's首次在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); 
    } 
    }); 
+0

不要添加irelevant或错误标签! – Olaf

+0

我不明白你的答案 – jcunham

回答

0
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); 
     } 
    }); 
+1

我使用webform不是mvc – jcunham