简体   繁体   English

Jquery Ajax 未将数据传递给 MVC Controller

[英]Jquery Ajax Not Passing Data to MVC Controller

I am using jquery's ajax method to submit data to a MVC controller.我正在使用 jquery 的 ajax 方法将数据提交到 MVC controller。 However, no data is passed to the controller.但是,没有数据传递到 controller。 I have tried altering the parameter expected by the controller to a string and the object expected.我尝试将 controller 预期的参数更改为字符串,并尝试将 object 更改为字符串。 When it is a string null is passed and when it is an object an empty object is created.当它是一个字符串 null 时被传递,当它是一个 object 时,一个空的 object 被创建。

The code below speaks for its self, but as a brief explanation I am creating a javascript object with customer data and stringifying that as part of the ajax call.下面的代码不言自明,但作为简要说明,我正在创建一个带有客户数据的 javascript object 并将其作为 ajax 调用的一部分进行字符串化。

I know there are previous posts on this, which I've followed but with no success.我知道以前有过这方面的帖子,我已经关注了这些帖子,但没有成功。

Any help greatly appreciated.非常感谢任何帮助。

 var data = {
                 id: _CustomerID,
               Address1 : _Address1,
               Address2 : _Address2,
               Address3 : _Address3,
               Address4 : _Address4,
               PostCode : _Address5,
               EmailAddress : _Email,
               PhoneNumber : _Phone,
               postemail: _postemail,
               postonly: _postonly,
               emailonly: _emailonly
         }
         url = '@Url.Action("UpdateContactDetails", "MyAccount")';

         $.ajax({
                type: "POST",
                dataType: "json",
                data: JSON.stringify({CustomerObj: data}),
                url: url,
                contentType: "application/json"
            }).done(function (res) 
            {
                //process result
            });

I used this website to test your Ajax request, You Have to send object parameters instead of JSON.stringify我用这个网站测试你的 Ajax 请求,你必须发送 object 参数而不是 JSON.stringify

Ajax Request Sample Code: Ajax 索取样品代码:

$.ajax({
  type: 'POST',
  dataType: 'json',
  data: {
    'name': 'morpheus',
    'job': 'leader'
  },
  url: 'https://reqres.in/api/users'
}).done(function(response) {
  console.log(response)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM