【问题标题】:Postman post request give proper result but ajax post give Count=0邮递员发布请求给出正确的结果,但 ajax 发布给出 Count=0
【发布时间】:2020-08-24 02:16:31
【问题描述】:

JS:

newRecords = [{
    "id": 7,
    "name": "Raddish",
    "rate": 30,
    "weight": "2",
    "amountperweight": 60
  },
  {
    "id": 8,
    "name": "Peas",
    "rate": 35,
    "weight": "4",
    "amountperweight": 140
  }
]

$.ajax({
  method: "POST",
  url: "http://localhost:36551/Orders/GenerateOrder",
  data: ({
    "generateOrder": newRecords
  }),
  contentType: "application/json, charset=utf-8",
  success: function(response) {
    console.log("response", response);
  },
  error: function(error) {}
});

Asp.Net:

 public class OrderCart
        {
            public int id { get; set; }
            public string name { get; set; }
            public int rate { get; set; }
            public string weight { get; set; }
            public int amountperweight { get; set; }
        }

    [HttpPost]
    [Route("GenerateOrder")]
    [ActionName("GenerateOrder")]
    public List<OrderCart> GenerateOrder(List<OrderCart> generateOrder)
    {
        return generateOrder;
    }

邮差回复:

VS 中的 Ajax Post 响应:

【问题讨论】:

    标签: jquery asp.net ajax api postman


    【解决方案1】:

    客户端代码如下所示:

    @section scripts{
        <script>
            $(function () {
                var generateOrder = [{ "id": 7, "name": "Raddish", "rate": 30, "weight": "2", "amountperweight": 60 },
                { "id": 8, "name": "Peas", "rate": 35, "weight": "4", "amountperweight": 140 }];
    
                $.ajax({
                    method: "POST",
                    url: "/Orders/GenerateOrder",
                    data: JSON.stringify(generateOrder),
                    contentType: "application/json, charset=utf-8",
                    success: function (response) {
                        console.log("response", response);
                    },
                    error: function (error) {
                    }
                });
            })
        </script>
    }
    

    服务器端操作将是:

    [HttpPost]
            public List<OrderCart> GenerateOrder(List<OrderCart> generateOrder)
            {
                return generateOrder;
            }
    

    现在 Visual Studio 应该可以正常工作了。

    【讨论】:

      【解决方案2】:

      改变行数据

      data: JSON.stringify([
      { 
          id: 0,
          name: '',
          rate: 0,
          weight: 0,
          amountperweight: 0
      },
      { 
          id: 0,
          name: '',
          rate: 0,
          weight: 0,
          amountperweight: 0
      },
      ...
      ]),
      

      如果变量 newRecords 是格式

          [
          { 
              id: 0,
              name: '',
              rate: 0,
              weight: 0,
              amountperweight: 0
          },
          { 
              id: 0,
              name: '',
              rate: 0,
              weight: 0,
              amountperweight: 0
          },
          ...
          ]
      

      这样做

      data: JSON.stringify(newRecords),
      

      【讨论】:

        猜你喜欢
        • 2019-08-10
        • 2021-11-04
        • 1970-01-01
        • 2018-07-12
        • 1970-01-01
        • 1970-01-01
        • 2018-07-22
        • 2018-01-22
        • 1970-01-01
        相关资源
        最近更新 更多