简体   繁体   中英

Jquery Ajax Not Passing Data to MVC Controller

I am using jquery's ajax method to submit data to a MVC controller. However, no data is passed to the controller. I have tried altering the parameter expected by the controller to a string and the object expected. When it is a string null is passed and when it is an object an empty object is created.

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.

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 Request Sample Code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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