【问题标题】:Error passing json to webmethod将 json 传递给 webmethod 时出错
【发布时间】:2013-04-16 20:37:35
【问题描述】:

我在将 json 传递给 webmethod 时出错了 服务器端,webmethod 需要一个字符串。 我认为错误是因为我试图在 json 中转换一个数组

function Salvar() {
  var arrayCursos = [];
  $("#dvCursos :checkbox").each(function(index) {
      if (this.checked) {
          var item = {
              idCurso: this.value,
              permi: "1"
          };
          arrayCursos.push(item);
      } else {
          var item = {
              idCurso: this.value,
              permi: "0"
          };
          arrayCursos.push(item);
      }
  });
   var jsonText = JSON.stringify({ jsonCursos: arrayCursos });
  $.ajax({
      type: "POST",
      url: "usuario-curso.aspx/Salvar",
      data : jsonText,
      async: false,
      contentType: 'application/json;charset=utf-8',
      success: function() {

      },
      error: function(status, ajaxOption, ex) {
          alert(ex);
      }
  });

}

【问题讨论】:

  • 你也应该发布你的网络方法?
  • 你在服务器端使用什么框架?

标签: jquery ajax json webmethod


【解决方案1】:

如果你的 webmethod 需要一个字符串,你需要告诉服务器改变你的 contentType

    function Salvar() {
    var arrayCursos = [];
    $("#dvCursos :checkbox").each(function(index) {
        if (this.checked) {
            var item = {
                idCurso: this.value,
                permi: "1"
            };
            arrayCursos.push(item);
        } else {
            var item = {
                idCurso: this.value,
                permi: "0"
            };
            arrayCursos.push(item);
        }
    });
    var jsonText = JSON.stringify({ jsonCursos: arrayCursos });
    $.ajax({
        type: "POST",
        url: "usuario-curso.aspx/Salvar",
        data : jsonText,
        async: false,
        contentType: 'text/html;charset=utf-8',
        success: function() {
        // some code here
        },
        error: function(status, ajaxOption, ex) {
            alert(ex);
        }
    });
}

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2014-07-06
    • 2017-03-02
    • 1970-01-01
    • 2012-02-04
    相关资源
    最近更新 更多