2

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;}
2
  • 2
    Your not returning json (your returning int) Make you method public JsonResult getNote { ..... return Json(yourIntValue); } and you can remove the coententType (sic) option and just use data { a: "a", b: "b" } Commented Nov 28, 2015 at 23:38
  • Thanks a lot. I thought the problem was in the View but it was in the Controller lol. Thanks again. Commented Nov 28, 2015 at 23:54

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.