23

I have the controller like the below:

public ActionResult Save(string input, string name) {
    //Some code
    return PartialView();
}

And I need an ajax call to this controller method and pass the two arguments input and value

And my ajax call is like the below:

$.ajax({
    url: '/Home/Save',
    type: 'POST',
    async: false,
    dataType: 'text',
    processData: false,
    data: "input=" + JSON.stringify(data) + "&name =" + $("#name").val(),
    success: function (data) {
    }
});

I am unable to pass the value to the name parameter. The value in the name parameter is becoming null.

starball
  • 20,030
  • 7
  • 43
  • 238
DS kumar
  • 347
  • 2
  • 4
  • 11
  • possible duplicate of [jquery Ajax call - data parameters are not being passed to MVC Controller action](http://stackoverflow.com/questions/2002163/jquery-ajax-call-data-parameters-are-not-being-passed-to-mvc-controller-action) – xdumaine Nov 25 '13 at 13:31

5 Answers5

29

You're making an HTTP POST, but trying to pass parameters with the GET query string syntax. In a POST, the data are passed as named parameters and do not use the param=value&foo=bar syntax. Using jQuery's ajax method lets you create a javascript object with the named parameters, like so:

$.ajax({
  url: '/Home/SaveChart',
  type: 'POST',
  async: false,
  dataType: 'text',
  processData: false,    
  data: { 
      input: JSON.stringify(IVRInstant.data), 
      name: $("#wrkname").val()
  },
  success: function (data) { }
});
xdumaine
  • 10,096
  • 6
  • 62
  • 103
5

In addition to posts by @xdumain, I prefer creating data object before ajax call so you can debug it.

var dataObject = JSON.stringify({
                    'input': $('#myInput').val(),
                    'name': $('#myName').val(),
                });

Now use it in ajax call

$.ajax({
          url: "/Home/SaveChart",
          type: 'POST',
          async: false,
          dataType: 'json',
          contentType: 'application/json',
          data: dataObject,
          success: function (data) { },
          error: function (xhr) { }            )};
Himalaya Garg
  • 1,525
  • 18
  • 23
0

I did that with helping from this question

jquery get querystring from URL

so let see how we will use this function

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

and now just use it in Ajax call

"ajax": {
    url: '/Departments/GetAllDepartments/',                     
    type: 'GET',                       
    dataType: 'json',                       
    data: getUrlVars()// here is the tricky part
},

thats all, but if you want know how to use this function or not send all the query string parameters back to actual answer

Community
  • 1
  • 1
Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
0
function final_submit1() {
    var city = $("#city").val();
    var airport = $("#airport").val();

    var vehicle = $("#vehicle").val();

    if(city && airport){
    $.ajax({
        type:"POST",
        cache:false,
        data:{"city": city,"airport": airport},
        url:'http://airportLimo/ajax-car-list', 
        success: function (html) {
             console.log(html);
          //$('#add').val('data sent');
          //$('#msg').html(html);
           $('#pprice').html("Price: $"+html);
        }
      });

    }  
}
Purnendu Sarkar
  • 332
  • 2
  • 12
-2
$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "ChnagePassword.aspx/AutocompleteSuggestions",
  data: "{'searchstring':'" + request.term + "','st':'Arb'}",
  dataType: "json",
  success: function (data) {
     response($.map(data.d, function (item) {
         return { value: item }
     }))
  },
  error: function (result) {
      alert("Error");
  }
});
Gilad Green
  • 36,708
  • 7
  • 61
  • 95