I am calling a ajax method as below
var srchText = "Chicago";
$.ajax({
url: "/Ajax/GetCities",
data: "{'srchText' : '" + srchText + "'}",
dataType: "json",
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
cityList = data.d;
}
});
The url is pointing to a MVC controller, as below,
[HttpPost]
public ActionResult GetCities(string srchText)
{
List<City> result = new List<City>();
EventsBIZ objBIZ = new EventsBIZ();
result = objBIZ.ToList<City>(objBIZ.GetCities(srchText));
return this.Json(new GetEventsResponse() { d = result }, JsonRequestBehavior.AllowGet);
}
There is something wrong with the code, that the method is called successfully, but the srchText is coming as null. Please help me to figure out wat went wrong. Thanks in advance
Adding the request tracked from firebug.

The reason your code doesn’t work is because by default ASP.NET MVC 2 doesn’t understand
JSONrequests. There is nothing built-in that allows you to send a JSON formatted request and that this request is parsed back to a strongly typed action argument. This functionality is built-in by default starting from ASP.NET MVC 3. Take a look at the following blog post. You will need to implement aJsonValueProviderFactoryif you want to make this work under ASP.NET MVC 2.Also instead of:
you should use:
The
JSON.stringifyis native for modern browsers, and for older you might need to includejson2.js.Another possibility if you don’t want to implement a
JsonValueProviderFactoryis to use a standardapplication/x-www-form-urlencodedrequest which the default model binder can understand: