jQuery calling an action

Asked

Viewed 156 times

1

I am developing a web site in ASP.NET MVC that uses jquery to control some plugins like spinner, toastr and others. My question is whether I should call Action via jquery to validate the model and within the Action make a Rest call (which was built in web api) and return the response in the javascript function.

As I worked a lot with mobile, most of the calls were made on the client, and in this case I see that the call is being made inside the controller, in the case, on the server.

  • This result action could not be an event in jQuery?

  • I don’t think so, it basically validates the model that was passed via post and executes a Rest call to Autentication. If OK, it generates the cookie and the User ticket

  • I don’t quite understand your scenario, would it be something like an Oauth? Where the client has previously authenticated itself in a Webapi that manages access to multiple applications, then when accessing the application, makes a request to this Webapi to validate the access to then proceed?

  • That’s right @Tobymosque. Do you think I’m doing it wrong? see this same scenario for my other controllers, who need to access the webapi where the call of the methods are made in the controller

  • I think you’re on the right track, but I advise you to create a custom Action filter for authentication and put all your logic into it.

1 answer

2


My question is whether I should call Action via jquery to validate the model and within the Action make a Rest call (which was built in web api) and return the response in the javascript function.

Web API is a great place to do this, but it’s good to make sure that the request header explicitly asks for a JSON:

$.ajax({
    dataType: ($.browser.msie) ? "text" : "json",
    accepts: {
        text: "application/json"
    },
    ...
});

Can be done on ASP.NET MVC as well, but do not use ActionResult to declare the Action. Use:

public JsonResult MinhaAction() { ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.