【问题标题】:jQuery: Mixing Strings and List of Javascript Objects in JSONjQuery:在 JSON 中混合字符串和 Javascript 对象列表
【发布时间】:2014-06-10 12:14:29
【问题描述】:

我想要做的可能很简单,但是我对 jQuery 不是很熟悉,我不知道该怎么做。

我想将一些数据作为 JSON 发送到 ASP.NET 控制器。数据包含一些字符串和对象列表。

代码看起来有点像这样:

查看:

    $(document).ready(function () {
    var stuff = [
        { id: 1, option: 'someOption' },
        { id: 2, option: 'someOther' },
        { id: 3, option: 'anotherOne' }
    ];

    things = JSON.stringify({ 'things': things });
    var dataRow = {
        'String1': 'A String',
        'String2': 'AnotherOne'
    }
    dataRow = JSON.stringify(dataRow);
    var sendData = dataRow + things;
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Backend/DoStuffWithStuff',
        data: sendData,
        success: function () {
            alert('Success!');
        },
        failure: function (response) {
            alert('Fail! :(');
        }
    });
});

控制器:

    public class Stuff
    {
        public int id { get; set; }
        public string option{ get; set; }
    }

    public void DoStuffWithStuff(string String1, String2, List<Thing> things)
    {
        //Do my Stuff
    }

任何想法都会很棒! :)

【问题讨论】:

    标签: javascript jquery asp.net json controller


    【解决方案1】:

    您不需要对 json 数据进行字符串化。 您只需创建一个您要发送的对象即可

    var jsonObject = {
       'string' : 'string',
       'object' : {
           'stirng': 'string'
       }
    };
    
    $.ajax({type: "POST", url: DotNetScript, data: jsonObject})
    .done(function(dataBack){
       //what to do with data back
    });
    

    【讨论】:

      【解决方案2】:

      到目前为止,它实际上看起来还不错!只是一些事情......

      [HttpPost]
      public void DoStuffWithStuff(string String1, String2, List<Stuff> things)
      {
          //Do my Stuff
      }
      

      在这里,你实际上并没有给 string2 一个类型。我假设这是一个错字,但这是次要部分。

      此外,在该方法中,请注意它的顶部有 HttpPost。在您的 javascript 中:

      $.ajax({
          ...
          type: 'POST',
          ...
      });
      

      您指定 POST,因此您必须使方法支持发布(在这种情况下,您也可以通过将类型更改为 GET,然后删除属性来摆脱 GET,但我不确定您的“东西”需要什么。 ..)

      var stuff = [
          { id: 1, option: 'someOption' },
          { id: 2, option: 'someOther' },
          { id: 3, option: 'anotherOne' }
      ];
      
      things = JSON.stringify({ 'things': things });
      var dataRow = {
          'String1': 'A String',
          'String2': 'AnotherOne'
      }
      dataRow = JSON.stringify(dataRow);
      var sendData = dataRow + things;
      

      您实际上并没有将东西传递到您的方法中,这可能会有所帮助...

      这是用正确的 JSON 传递重写的 ajax 方法(对于您在此处尝试执行的操作)。

      $(document).ready(function () {
          var stuff = [
              { id: 1, option: 'someOption' },
              { id: 2, option: 'someOther' },
              { id: 3, option: 'anotherOne' }
          ];
          var dataRow = {
              String1: 'A String',
              String2: 'AnotherOne'
              things: stuff
          }
          $.ajax({
              dataType: 'json',
              type: 'POST',
              url: '/Backend/DoStuffWithStuff',
              data: sendData,
              success: function () {
                  alert('Success!');
              },
              failure: function (response) {
                  alert('Fail! :(');
              }
          });
      });
      

      【讨论】:

      • 非常感谢!我不得不修改一些东西,但现在它就像一个魅力。我不得不删除 contentType 值,因为它由于某种原因破坏了一切,我需要删除我的 dataRow 中的 JSON.stringify。现在一切正常!
      • 没问题!感谢您的注意,我会为其他人更新我的答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2018-05-01
      相关资源
      最近更新 更多