Home > database >  Jquery Ajax Not Passing Data to MVC Controller
Jquery Ajax Not Passing Data to MVC Controller

Time:02-02

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
            });

CodePudding user response:

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)
});
  •  Tags:  
  • Related