1

I want read returned value from this method:

public ActionResult ClientIsBlocked(int? clientId)
{
    if (!clientId.HasValue)
        return Json(null);

    bool isBlocked = false;

    try
      {
         isBlocked = this.clientsProvider.GetClientById(clientId.Value).IsBlocked;
       }
    catch
       {
           // logg
       }
    return Json(isBlocked);
 }

in java script in my view. It should be async/ajax. How to do that? It is my js code in view.

function isBlocked(id) {
            $.ajax({
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                type: "GET",
                url: '@Url.Action("ClientIsBlocked", "CustomerManagement", new { Area = "CustomerManagement" })',
                data: JSON.stringify({ 'clientId': id }),
                success: function(data) {
                    if(!data.success) {

                    }
                }
            })
4
  • please post your Ajax call method Commented Sep 2, 2015 at 11:39
  • BTW, the try {} catch {} really should do something if it is the production code... Not just eating the exception. Commented Sep 2, 2015 at 11:44
  • The data inside your success should be boolean type, the isBlocked value. Just check this like this console.assert(typeof(data) == "boolean") Commented Sep 2, 2015 at 11:46
  • in your success function don't use data.suceess because data directly contains a boolean value of isBlocked property returned from server. Also return return Json(isBlocked,JsonRequestBehavior.AllowGet); form server as you are missing JsonRequestBehavior.AllowGet Commented Sep 2, 2015 at 11:49

1 Answer 1

1

In your action change :

return Json(isBlocked);

to:

return Json(isBlocked,JsonRequestBehavior.AllowGet);

otherwise your ajax may fail throwing exception:

Server Error in '/' Application.

This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.

and in js in success callback:

success: function(data) {

           alert(data); // data is the bool that is returned by action
          }
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.