leddy
leddy

Reputation: 553

Ajax is not calling the webmethod

I'm trying to get this loadResources function to work so I have put it behind the call of a button - the function is called but the ajax always fail (i get the "loading of Engineers failed!" alert) and I can't work out why

loadResources = function (instructionID) {

    var objResource = {};

    objResource.instructionID = instructionID;
    objResource.ignorePostcode = true;
    objResource.pageIndex = 1;
    objResource.pageSize = 10;
    objResource.totalRows = 0;

    var requestResource = $.ajax({
        type: "POST",
        url: "NewJob.aspx/getResourcesDayShift",
        data: JSON.stringify(objResource),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
    requestResource.done(function (data) {

        alert(data);

    });
    requestResource.fail(function () {
        //$.unblockUI();
        alert('loading of Engineers failed!');
    });
}

this is the method I am trying to call:

   [WebMethod]
    public static string getResourcesDayShift(Guid instructionID, bool ignorePostcode, int pageIndex, int pageSize, ref int totalRows)
    {
        return "blah";

    }

Upvotes: 0

Views: 147

Answers (2)

bhanu.cs
bhanu.cs

Reputation: 1365

Javascript is always pass by value. So passing reference parameters to your web method is not valid..

In javascript convert the Guid into a string array object and pass that string array object to the web method.

On your client side.

var ie = ['11111111 - 1111 - 1111 - 1111 - 111111111112','14897bf4-f5d0-4548-a74d-a34f00ae9cad'];
 var objResource = { instructionID: ie, ignorePostcode: true, pageIndex: 1, pageSize: 10, totalRows: 0 };



 [WebMethod]
  public static string getResourcesDayShift(string[] instructionID, bool ignorePostcode, int pageIndex, int pageSize, int totalRows)
        {
            return "blah";

        }

Upvotes: 0

Grundy
Grundy

Reputation: 13380

When you call this method you get next error message

Message: "Cannot convert object of type 'System.Int32' to type 'System.Int32&'"

This problem with ref parameters of your method.

If you want return some data, you can do next change

[WebMethod]
public static object getResourcesDayShift(Guid instructionID, bool ignorePostcode, int pageIndex, int pageSize, int totalRows)//return ref keyword
{
    return new { value = "blah", totalRows=totalRows+1 };

}

and on client side you get

requestResource.done(function (data) {

    console.log(data.d); //Object {value: "blah", totalRows: 1}

});

Upvotes: 1

Related Questions