Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a jquery ajax script like following

    $.ajax({
            type: "POST",
            url: "Main/receive", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ 'p':$("#txtname").val() }),
            dataType: "json",
            success: function (result) {
                alert('Yay! It worked!');
                // Or if you are returning something

            },
            error: function (result) {
                alert('Oh no zzzz:('+result.responseText);
            }
        });

And I am calling to Controller action method. The data is sending to the controller's action method and I am also receiving data from the controller. But the data that I am receiving is inside the error function of jquery ajax.

I want it to be inside the success function.

Why my success function is not getting called.

Following is my controller's action method,

   [HttpPost]
    public string receive(string p)
    {
        ViewBag.name = p;
        return p;

    }
share|improve this question
1  
Because you have specified that the return type is json (i.e. dataType: "json",). Change the server method to return Json(p); But there are lots or other potential errors in your code so I will post an answer later. – Stephen Muecke May 23 '15 at 7:22
    
@StephenMuecke thanks, please don't forget to post answer – Alex May 23 '15 at 7:49
up vote 1 down vote accepted

The reason for the error is that you have specified that the returned data type be json (in the line dataType: "json",) but you method returns text. You have 2 options.

  1. Change the controller method to return json using return Json(p);
  2. Change the ajax option to dataType: "text", or just omit it

However you can improve your script as noted below

$.ajax({
  type: "POST",
  url: '@Url.Action("receive", "Main")', // don't hardcode url's
  data: { p: $("#txtname").val() }, // no need to stringify (delete the contentType: option)
  dataType: "json",
  success: function (result) {
      alert('Yay! It worked!');
  },
  error: function (result) {
      alert('Oh no zzzz:('+result.responseText);
  }
});

or even simpler

$.post('@Url.Action("receive", "Main")', { p: $("#txtname").val() }, function(result) {
    alert('Yay! It worked!');
}).fail(function(result) {
    alert('Oh no zzzz:('+result.responseText);
});

Notes: You should always use @Url.Action() to generate the correct urls, and it is not necessary in this case to stringify the data (but you need to remove the contentType: line so it used the default application/x-www-form-urlencoded; charset=UTF-8)

In addition, this is not strictly a POST (your not changing data on the server - but I assume this is just for testing). And there is no point in the line ViewBag.name = p; - it does nothing in your context, and as soon as you return from the method, ViewBag is lost anyway.

share|improve this answer
    
I had a typo in the error line - see edit, but that error does not make sense (I tested the code in my project and it works fine). I'll see if I can find some info on that error – Stephen Muecke May 23 '15 at 9:11
    
it's working. thanks – Alex May 23 '15 at 9:12
    
I have asked a new question. can you please check it out?stackoverflow.com/questions/30420765/… – Alex May 24 '15 at 6:38

try to change your controller code as follows

[HttpPost]
 public ActionResult List(string p)
    {
       ViewBag.name = p;
       return Json(ViewBag);
    }
share|improve this answer
    
it's giving error. can not convert p string to type object – Alex May 23 '15 at 7:27

Your controller method should look like this:

[HttpPost]
public ActionResult receive(string p)
{
   return Json(p);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.