1

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;

    }
2
  • 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. Commented May 23, 2015 at 7:22
  • @StephenMuecke thanks, please don't forget to post answer Commented May 23, 2015 at 7:49

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
I have asked a new question. can you please check it out?stackoverflow.com/questions/30420765/…
0

try to change your controller code as follows

[HttpPost]
 public ActionResult List(string p)
    {
       ViewBag.name = p;
       return Json(ViewBag);
    }

1 Comment

it's giving error. can not convert p string to type object
0

Your controller method should look like this:

[HttpPost]
public ActionResult receive(string p)
{
   return Json(p);
}

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.