Search code examples
c#asp.netajaxwebmethod

Ajax request cannot access method on .aspx file


I'm trying to call a method in my .aspx file to call another .aspx file. The file that I am using is inside another folder, and the file I need to access using javascript is outside similar to this structure:

Root Folder:
PageMethods.aspx
    SubFolder/
       Somefile.aspx

Here is my ajax request:

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/PageMethods.aspx/TagtoVehicle",
        data: "{'colIDBigint': '" + IDBigInt + "', 'colTravelReqIDInt': " + TravelReq +
            ", 'colRecordLocatorVarchar': " + RecordLoc + ", 'colSeafarerIdInt': " + SeafarerId + ", 'colOnOff': " + onoff + ", 'colPortAgentVendorIDInt': " + Vehiclevendor + ", 'UserId': " + userId + "}",
        dataType: "json",
        success: function(data) {
        } ,
        error: function(objXMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

When I run the ajax request it throws an error 500 (Internal Server Error).

Why is the request not going thru, even if inside my PageMethod.aspx.cs is this:

[WebMethod]
        public static string TagtoVehicle(Int32 colIDBigint, Int32 colTravelReqIDInt, string colRecordLocatorVarchar, Int32 colSeafarerIdInt, string colOnOff, Int32 colPortAgentVendorIDInt,string UserId)
        {

}

What is triggering the error 500?


Solution

  • You are missign quotes around values.Just like you have encoded value of colIDBigint do same for all of the remaning

    data: "{'colIDBigint': '" + IDBigInt + "', 'colTravelReqIDInt': '" + TravelReq +
            "', 'colRecordLocatorVarchar': '" + RecordLoc + "', 'colSeafarerIdInt': '" + SeafarerId + "', 'colOnOff': '" + onoff + "', 'colPortAgentVendorIDInt': '" + Vehiclevendor + "', 'UserId': '" + userId + "'}",
    

    Better is that you stringify the data like this

    JSON.stringify({colIDBigint:IDBigInt,colTravelReqIDInt:TravelReq})
    

    and like that add other values in data.