0

I'm trying to call an ASP.NET MVC controller using an AJAX Call. Basically I want to return customer details based on the ID selected from a dropdownlist. On debugging, controller is being hit and returning data in JSON format however, on debugging javascript, it never gets to success, failure or error.

Here is the code I'm using:

View:

<script type="text/javascript">
    $(document).ready(function () {
        $("#CustomerId").select2({
            placeholder: "Select a customer"
        });

        $("#CustomerId").change(function () {
            var param = $("#CustomerId Option:Selected").val();
           
            $.ajax({
                type: 'GET',
                data: { Id: param },
                url: '/QuickInvoices/GetCustDetails',
                
                success: {
                    function(response) {
                       
                        if (response != null) {
                            alert("hello");
                            $('customer_CompanyName').val(response.CompanyName);
                        }
                        else {
                            alert("something went wrong!");
                        }
                    }
                },
                failure: function (response) {
                    alert('failed');
                },
                error: function (response) {
                    alert('error' + response.responseText);
                }
            });
        });
    });
</script>

Controller:

[HttpGet]
public JsonResult GetCustDetails(int Id)
{
    Customer customer = db.Customers.Where(x => x.Id == Id)
                                    .SingleOrDefault<Customer>();

    return Json(customer, JsonRequestBehavior.AllowGet);
}

Can someone help please?

4
  • Any errors in your browser's Console or Network tools? Commented Jan 12, 2021 at 21:43
  • is the url different when not debugging? Commented Jan 12, 2021 at 22:07
  • No errors in the console / network tools since i even tried debugging on chrome. Url is the same both in debug and release mode Commented Jan 13, 2021 at 5:58
  • Remove the excess scope definition after "success" event as mentioned by @Amit Sakare in the response below. I tried running your code by removing the excess curly braces in discussion and it worked fine. Commented Jan 15, 2021 at 9:50

1 Answer 1

1

Please try below Code sample and check the Request & Response of network tab you will get batter Idea

 $(document).ready(function () {
        $("#CustomerId").select2({
            placeholder: "Select a customer"
        });
        $("#CustomerId").change(function () {
            var param = $("#CustomerId Option:Selected").val();
                   $.ajax({  
                        type: "POST",  
                        url: '@Url.Action("GetCustDetails", "QuickInvoices")',
                        data: { Id: param },  
                        dataType: "json"  
                        contentType: 'application/json; charset=utf-8',  
                        success: function(data) {  
                            alert(data.msg);  
                        },  
                        error: function() {  
                            alert("Error occured!!")  
                        }  
                    });  
        });
    });
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.