0

I am using an ajax call to retrieve data. The ajax call does not seem to get to the controller and I am getting an error saying undefined

My ajax call on change of dropdown

$(document).ready(function () {
    $('body').on('change', '#qualificationDrpdown', function () {
        alert($("#qualificationDrpdown").val())

        $.ajax({
                type: "GET",
                url: "Home/SelectStudyModes",
                contentType: "application/json; charset=utf-8",
                data: { "p": $("#qualificationDrpdown").val() },
                //data: { "p": "2" },
                dataType: "json",
                success: function (result) {
                    alert(result.data); 
                    alert("success");
                    $("#modesDrpdown").html(""); // clear before appending new list
                    $.each(studyModes, function (i, result) {
                        $("#modesDrpdown").append(
                            $('<option></option>').val(result.Id).html(result.StudyModeName));
                    });

                },
                error: function (result) {
                    alert('error:(' + result[0]);
                }

            });
        })
  });

My controller

[HttpGet]
public ActionResult SelectStudyModes(string p)
{
        int qualificationId = Convert.ToInt32(p);
        var Qualifications = app.GetQualifications();
        var StudyModes = Qualifications.Find(q => q.QualificationId == qualificationId).StudyModes;
        //GetStudymodeByQualificationID(qualificationId);
        ViewBag.name = p;
        return Json(StudyModes, JsonRequestBehavior.AllowGet);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • 1
    Is there any reason why it has to be a `GET`. Can you not remove the `HttpGet` from the function and change the ajax to a `type: 'POST'` seeing as you are posting data to the method? – JamesS Feb 26 '20 at 09:27
  • 1
    If the above does not work , you can also look at https://stackoverflow.com/questions/9988634/ajax-call-into-mvc-controller-url-issue/9988672 – Deepankur Singh Feb 28 '20 at 04:55
  • This a method that return a list of data so I think that using `GET` is not bad at all. However, the type of the request is `GET` so the `data` will be sent a query string. Maybe you should remove `dataType: "json"`. This is no use here I doubt. – Zan RAKOTO Feb 28 '20 at 06:20

1 Answers1

0

Kindly change the url and add type as below.

url: "/Home/SelectStudyModes",
type: "get",
Niraj
  • 89
  • 1
  • 5