【问题标题】:How to pass the object in $.ajax如何在 $.ajax 中传递对象
【发布时间】:2018-04-09 10:47:52
【问题描述】:

大家好,以下都是我的对象

 $scope.myObject= {
    ID: { "value": "", "column": "ID" },
    Name: { "value": "", "column": "Name" },
    Desc: { "value": "", "column": "Desc" },
    IsActive: { "value": "", "column": "IsActive" },
    CreatedBy: { "value": "", "column": "CreatedBy" },
    ModifiedBy: { "value": "", "column": "ModifiedBy" },
    CreatedDate: { "value": "", "column": "CreatedDate" },
    ModifiedDate: { "value": "", "column": "ModifiedDate" },
    ConGp: { "value": "", "column": "ConGp" },
    SortIndex: { "value": "", "column": "SortIndex" },
    PageNumber: { "value":"", "column": "PageNumber" },
    PageSize: { "value": "", "column": "PageSize" }
}

value 将从 html 文本框中填充 我想在ajax调用中传递这个对象

  $.ajax({
                    url: url,
                    type: "GET",
                    data: JSON.stringify($scope.myObject),
                     success: function (data) {

                        $scope.FRP= data.RM;
                        $scope.$apply();
                    }

但是在 MVC 控制器端数据总是得到空值,但是在发送对象 $scope.myObject 时,值被填充,它在 ajax 调用中丢失了。请提出建议。

【问题讨论】:

  • 你甚至想通过 jQuery 发送一个绑定到 Angular 的对象,这很奇怪......? Try this instead.
  • 1.您正在使用 angularjs,因此将 $.ajax 替换为 $http。 2. 你需要一个 POST 请求,所以请改用$http.post。例如。 $http.post(url,$scope.myObject).then((res) => {$scope.FRP = res.data.RM; ...})
  • @RoryMcCrossan:即使我将 $.ajax 替换为 $http 我的对象值在 $http 调用中丢失了。
  • 你能添加一些网络服务代码吗?
  • lost 不知道你的意思是什么。你能在网络开发工具中检查你的请求正文吗?

标签: jquery angularjs ajax


【解决方案1】:

如果要发送数据,请使用“POST”方法。 $.ajax 或 $http.post

【讨论】:

    【解决方案2】:

    为了向数据库/服务器发送数据,我们需要使用 post 请求。 如果您使用的是 MVC 框架,我建议您使用[axios][1]。 这是Promise based HTTP client for the browser。

    对于你的问题:-

    $.ajax({
      url: url,
      type: "POST",
      data: JSON.stringify($scope.myObject),
      success: function(data) {
        $scope.FRP = data.RM;
        $scope.$apply();
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-13
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2021-10-30
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多