0

I am creating a post request using jquery and I am not getting the expected results. I am just trying to return true.

Here is my code:

function deleteAllUsers() {
    $.post('/Account/DeleteAllUsers', function (data) {
        alert('post');
        alert(data);
    });
}

and

public JsonResult DeleteAllUsers()
    {
        var t = System.Web.Helpers.Json.Encode(new {ts = true});
        return Json(t);
    }

I've also just tried this in the code behind:

public JsonResult DeleteAllUsers()
    {
        return Json(true);
    }

And every time the "alert(data)" returns a string of the entire web page. What am I doing wrong? Why is this not return true?

Also, how do I get the debugger to step through the code behind when I am making an ajax call?

11
  • To debug the server side handling of the call, in VStudio, Debug --> Attach To Process --> Attach to w3p if you are hosting in IIS.
    – Bill Gregg
    Jun 5, 2013 at 18:28
  • Entire Page means? Entire Error Page I guess? Jun 5, 2013 at 18:30
  • No, not the error page. It is just a string of the html for the current page that I have loaded.
    – dcinadr
    Jun 5, 2013 at 18:32
  • How are you invoking deleteAllUsers? Is it an anchor tag link?
    – Jasen
    Jun 5, 2013 at 18:32
  • I don't know ASP, but isn't it: var t = System.Web.Helpers.Json.Encode(new {ts : true});
    – A. Wolff
    Jun 5, 2013 at 18:33

4 Answers 4

0

If your debugger isn't hitting your break point I would guess that you're not hitting your method. It sounds like your ajax call is hitting the current url you're on. If you inspect your ajax request with fiddler or firebug can you confirm that you're hitting your action method? Perhaps you need to change your javascript to $.get('/account/deleteallusers',blah blah blah....

2
  • i will try this. But I read that you should use GET for calls like this. I thought you should only use GET when you are retrieving data.
    – dcinadr
    Jun 5, 2013 at 18:39
  • 1
    correct you should use post. You'll need to have the [HttpPost] attribute above your method like @Jeremy Boyd mentioned . In this case you really should inspect the request to see what url you're actually calling from your script.
    – bluetoft
    Jun 5, 2013 at 18:41
0

Place the [HttpPost] attribute above the method you are trying to call, like so:

[HttpGet]
public ActionResult DeleteAllUsers()
{
    return View();
}
[HttpPost]
public JsonResult DeleteAllUsers()
{
    return Json(true);
}
3
  • Are you sure you are in the right controller? is DeleteAllUsers() under AccountController? Jun 5, 2013 at 18:39
  • Yes. It is in AccountController.
    – dcinadr
    Jun 5, 2013 at 18:40
  • What happens if you put [HttpGet] on the page's method? Jun 5, 2013 at 18:46
0

I finally figure it out...

The problem was that DeleteAllUsers wasn't authenticated. So the response was the Login page. It was requesting that I login in order to access the DeleteAllUsers function. So I authorized access to this function.

<location path="Account/DeleteAllUsers">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>

And I decorated my method like this...

    [HttpPost]
    [AllowAnonymous]
    public JsonResult DeleteAllUsers()
    {
        return Json(true);
    }

Thanks for the help everyone!

2
  • 1
    Hm... "DeleteAllUsers" doesn't seem like functionality I would make available to anonymous users...
    – Jason P
    Jun 5, 2013 at 20:34
  • It's just for testing some initial setup. Don't worry. It won't be available on the real product. :)
    – dcinadr
    Jun 5, 2013 at 20:56
0

You need to AllowGet on the returning Json

return Json(new { ts = true }, JsonRequestBehavior.AllowGet);

If this not work, can u post the message/html that "data" contains?

EDIT: Sorry about the get/post detail :(

Btw, u shouldn't return true or false to know if your HttpPost works or not, instead, u should use HTTPStatusCode for that;)

Something like:

return new HttpStatusCodeResult(HttpStatusCode.OK);
4
  • 1
    AllowGet is not required for an HTTP POST Jun 5, 2013 at 18:40
  • I read that you only need AllowGet if you are making a GET request and I am using POST
    – dcinadr
    Jun 5, 2013 at 18:40
  • Sorry about that confusion about get/post. See my edit about not using true/false for this kind off success check. I think in most off the cases, u should return the correct HttpStatusCode besides a boolean value. Jun 5, 2013 at 18:49
  • I am just returning true false to return something. Right now I don't care what it is I just want it to be something which is why I am just returning true.
    – dcinadr
    Jun 5, 2013 at 19:02

Your Answer

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

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