2016-03-01 5 views
-1

Мне нужна помощь. Я делаю webmethod в asp.net, но я всегда получал сообщение «Не удалось загрузить ресурс: сервер ответил со статусом 500 (Internal Server Error)»webmethod asp.net статус 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); 
    } 
    }); 
+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

Смежные вопросы