简体   繁体   中英

.NET MVC posting multiple parameters to controller using ajax

I know this question has been asked many times, I relly tried to follow many examples but every time, I fail for an unknown reason. So I'm going to show you my example a (very simple one) and I need someone to tell me what did I do wrong.

Starting with the controller (its name is Recherche) method:

public int getNote(string a,string b)
{
    if(string.IsNullOrEmpty(a))
        return 1;
    else return 0;
}

As you can see I didn't use the variable b, but who cares it's just an example.

Now for the ajax method:

$.ajax({
            type: "POST",
            url: "/Recherche/getNote",
            coententType: 'application/json',
            dataType: 'json',
            data:JSON.stringify({a:"a",b:"b"}),
            success: successFunc,
        });

        function successFunc(data) {
            document.getElementById('note').innerHTML = data;}

Try this

 var a1='';
 var b1='';
 $.ajax({
         type: "POST",
         url: "/Recherche/getNote",
         coententType: 'application/json',
         dataType: 'json',
         data:JSON.stringify({a:a1,b:b1}),
         contentType: "application/json; charset=utf-8",
         processData: false,
         success: function (data) {                        
                            document.getElementById('note').innerHTML = data;                            
                },
                error: function (response) {
                    if (response != 1) {
                        alert("Error!!!!");                           
                    }
                } 
         });

Controller

[HttpPost]
[WebMethod]
public ActionResult getNote(string a,string b)
{
   if (a== null && b==null) return Json(0);
      //Some Action  send Result         
   return Json(data, JsonRequestBehavior.AllowGet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM