locked
calling controller with ajax is failing RRS feed

  • Question

  • User-1173471724 posted

    I'm calling this controller using ajax

    public JsonResult GetSchedule(int id, int idClient, int idPolyclinic)
            {
    //whatever goes here
    //the return type is json
    return Json(result, JsonRequestBehavior.AllowGet);
    }

    and this is my ajax call

    function GetScheduleTime(select) {
        
        var shceduleDate = select.options[select.selectedIndex].getAttribute("value");
        var idCategory = $("#idCategory").val();
        var idPolyclinic = $("#idClient").val();
        var idPolyclinicDoctor = $("#idPolyclinicDoctor").val();
        var data = {};
       
            data = {
                id: idCategory,
                idClient: idPolyclinicDoctor,
                idPolyclinic: idPolyclinic
            };
    
       
       
        console.log(data);
        //get the times of this date
        $.ajax({
            type: 'Get',
            url: '/Appointments/GetSchedule',
            data: data,
            success: function (response) {
    
             //success code goes here
            },
            error: function (error) {
                alertify.set('notifier', 'position', 'top-right');
                alertify.error('something went wrong please try again later.');
            }
        });
       
    }

    when debugging and using the breakpoint I notice that the id, idClient are correct values, but the idPolyclinic contains nothing(no value, nor null) just nothing.

    although I'm sure the data of the ajax call is carrying the correct data.

    This is the console.log output result:

    Object
    id: "2"
    idClient: "8
    "idPolyclinic: "1"
    
    

    can anyone please explain to me what am I missing or what is going on?

    Sunday, December 16, 2018 8:52 AM

All replies

  • User1120430333 posted
    Object
    id: "2"
    idClient: "8
    "idPolyclinic: "1"

    If the above is supposed to be the actual Json object, it doesn't look valid to me with a missing double quote, a malformed Json object. 

    Sunday, December 16, 2018 12:36 PM
  • User-1173471724 posted

    its a typo error

    Sunday, December 16, 2018 3:13 PM
  • User1120430333 posted

    when debugging and using the breakpoint I notice that the id, idClient are correct values, but the idPolyclinic contains nothing(no value, nor null) just nothing.

    idPolyclinic  you have to figure out what is happening with its value that is probably causing it to not work. You can change the signature of the action method to one or two parms the ones that have values in them and send the object as a test case to see if the action method is reached.

    Sunday, December 16, 2018 5:33 PM
  • User-474980206 posted

    that's not possible based on your supplied code:

    public JsonResult GetSchedule(int id, int idClient, int idPolyclinic)

    idPolyclinic is an int which is a value type. it can not be null or nothing (is allocated as a zero). if never set by the post data, or if the post back data was invalid, it would be zero. maybe your source is out of sync. do a clean, build, and try debugging again.

    Sunday, December 16, 2018 7:58 PM
  • User1520731567 posted

    Hi lolo123,

    I suggest that you better standardize your code so that you can avoid many unnecessary errors.

    In addition to paying attention to type error, also pay attention to other attributes.

    Such as: the right url, Get or Post,JsonResult or ActionResult,and the type of the result of action...

    Best Regards.

    Yuki Tao

    Monday, December 17, 2018 2:54 AM