Reputation: 61
I tried doing the most simple ajax post sending string from the ajax and getting it in the controller. the controller method invoked but the string parameter is null.
This is the controller:
public ActionResult FilterByName(string s)
{
Here I put the break point.
This is the Jquery code:
$.get('/Filter/FilterByName', "ttt");
I realy don't know what I'm doing wrong. I'm In this for a day!!!
Upvotes: 0
Views: 56
Reputation: 378
As tymeJV commented, since your encoding your data and sending it via HTTP you need to have your data in a key/value pairing. Therefore
$.get('/Filter/FilterByName', "ttt");
should be
$.get('/Filter/FilterByName', {s: "ttt"});
Also ensure that your method is accepting get requests, otherwise by default it should be rejecting AJAX requests. This is due to the 'DenyGet' that is the default by MVC. With that being said, that is probably the next hurdle you're going to run into (as did I when I was first learning this). For example:
[HttpGet]
public ActionResult FilterByName(string s)
{
// some logic using s
....
return Json(result, JsonRequestBehavior.AllowGet);
}
Upvotes: 1