1

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. FireBug Net monitor

2
  • What do your routes look like? Couldn't you be using ("/Ajax/GetCities/" + srchText)? Commented Jun 25, 2011 at 18:04
  • updated with the firebug screenshot. I want to send it as json object, gradually i want to make the input as a complex class. So sending it through the url wont help me. Commented Jun 25, 2011 at 18:18

3 Answers 3

4

The reason your code doesn't work is because by default ASP.NET MVC 2 doesn't understand JSON requests. 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 a JsonValueProviderFactory if you want to make this work under ASP.NET MVC 2.

Also instead of:

data: "{'srchText' : '" + srchText + "'}",

you should use:

data: JSON.stringify({ srchText: srchText }),

The JSON.stringify is native for modern browsers, and for older you might need to include json2.js.

Another possibility if you don't want to implement a JsonValueProviderFactory is to use a standard application/x-www-form-urlencoded request which the default model binder can understand:

$.ajax({
    url: '/Ajax/GetCities',
    data: { srchText: srchText },
    type: 'POST',
    async: false,
    dataType: 'json',
    dataFilter: function (data) { return data; },
    success: function (data) {
        cityList = data.d;
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

U hit the nail on the head. One more question. When i change the input to an object instead of a string, and am using the default model binder, sending a normal javascript object is coming to the server. But the properties of the object are defalted(as in int to 0 and string to null). Any idea how to send the object without jsonvalueproviderfactory?
1

you don't have to send srchText as json becuase you will just send a string so can send it as query string

try this

 var srchText = "Chicago";


$.ajax({
url: "/Ajax/GetCities",
data: 'srchText=' + srchText ,
type: "POST",
async: false,
dataFilter: function (data) { return data; },
success: function (data) {
    cityList = data.d;
}
});

Comments

0

I don't think you're passing valid JSON, try using:

data: {"srchText" : srchText},

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.