Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:09:19+00:00 2026-06-15T21:09:19+00:00

I am trying to call an action from jQuery. The action should return true

  • 0

I am trying to call an action from jQuery. The action should return true or false,
and based on the return value I will do something.

I have the following code.

 $("#payment").click(function (e) {
       if($("#deliverytime").attr("disabled")){
            //something here...   
        }
        else
        {
            var postcode=$('#Postcode').val();
            var restId='<%:ViewBag.RestaurantId %>';
            var URL = "/Restaurant/DoesRestaurantDeliver?restid="+restId+"&&postcode="+postcode;

            $.ajax({
                 type: "GET",
                 url: URL
             }).done(function(msg) {
                        if(msg==true){
                             $('#commonMessage').html(msg);
                             $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
                             return false;
                        }
                        else{
                            $('#commonMessage').html(msg);
                             $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
                             return false;
                        }
            }); 



        }
    });

The code is not working. It says 'msg' is not defined. Is this not the way I should do this using jQuery-ajax? What am I doing wrong?

EDIT:

Controller action

public JsonResult DoesRestaurantDeliver(Int32 restid, string postcode)
    {

        if (rest.getDeliveryPriceForPostcode(restid, postcode) == null)
        {
            return Json(Boolean.FalseString, JsonRequestBehavior.AllowGet);
        }
        else
            return Json(Boolean.TrueString, JsonRequestBehavior.AllowGet);
    }

Modified function

var postcode = $('#DeliveryInfo_Postcode').val();
            var restId = '<%:ViewBag.RestaurantId %>';
            var URL = "/Restaurant/DoesRestaurantDeliver?restid=" + restId + "&&postcode=" + postcode;
            $.ajax({
                url: URL,
                dataType: "json",
                type: "GET",
                data: "{}",
                success: function (data) {
                    if (!data.hasError) {
                        $('#commonMessage').html(data);

                        return false;
                    }
                }
            });

EDIT -2

<script>
$(document).ready(function () {
    $("#checkout").click(function (e) {
        getDeliveryInfo();
    });
});
function getDeliveryInfo() {
    var URL ="/Restaurant/DoesRestaurantDeliver/6/2259"
    $.get(URL, function (data) {
        alert(data.isValid);
    });
}
</script>
<%using (Html.BeginForm())
 { %>
<input type="submit" name="submit" id="checkout"/>
<%} %>

The above code does not work. But If i put the ‘checkout’ button out side of the form like below, it works.

<%using (Html.BeginForm())
 { %>

<%} %>
<input type="submit" name="submit" id="checkout"/>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:09:20+00:00Added an answer on June 15, 2026 at 9:09 pm

    You can configure your ajax call like below.

    I feel your ajax call having problems on .done(function(msg) {} area.

    Try to set success: function (data) {} like below.

    <script type="text/javascript">
    
        $(function () {
    
            var companyName = $("#companyname");
    
            $.ajax({
                url: "/Company/GetName",
                dataType: "json",
                type: "GET",
                data: "{}",
                success: function (data) {
                    if (!data.hasError) {
                        companyName.html(data.companyName);
                    }
                }
            });
        });
    
    </script>
    

    EDIT

    Action method should looks like below (This is a sample.Adjust it according to your situation)

     [HttpGet]
     public JsonResult GetName(string term)
            {
                var result = Repository.GetName(term);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    

    EDIT 2

    I would use Anonymous object (isValid) (remember that JSON is a key/value pairs):

    Action Method

       [HttpGet]
       public JsonResult DoesRestaurantDeliver(Int32 restid, string postcode)
        {
           if (rest.getDeliveryPriceForPostcode(restid, postcode) == null)
            {
                return Json(new { isValid = false},JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { isValid = true },JsonRequestBehavior.AllowGet);
        }
    

    And then inside the ajax method:

    success: function(data) {
        if (data.isValid) {
            ...
        }
    }
    

    EDIT 3

    Just change your button type as below.(since you’re not sending any values from form)

    <input type="button" name="submit" id="checkout"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a server-side action in a controller from jQuery: $.ajax({ url:'http://localhost:88/admin/business/11/GetChildBusinessTypes',
I have been trying to get a jquery ajax function to return data from
I am trying to call a controller action from a jquery UI dialog. What
I am trying to use jQuery.parseJSON to parse out the return value from an
Im trying to build call to action button on my site using jQuery. I
Im trying to call a method that will add or subtract 1 from a
I am trying to call c# function from javascript the code i have tried
I am trying to achieve a JQuery AJAX call to a controller action method
I'm trying to call an action from a different controller's view: Controller Countries has
I am trying to call call action script function from JS but i get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.