Jayce
Jayce

Reputation: 595

Invalid web service call, missing value for parameter with asmx web service

I have this error : "Invalid web service call, missing value for parameter " when I call a web service method with a parameter.

I'm testing with a web service method without parameter that returns the same type of object and it works well.

Here is my web service method :

 [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public ResponseStatistic_3 Statistic_3(string klant)
    {
            Statistic_3[] items = Helper.Helper_Statistic_3(klant).ToArray();
            ResponseStatistic_3 response = new ResponseStatistic_3(items);


            return response;
    }

Here is my javascript code, I retrieve the good value in kla variable :

 function getStatistic3() {

var response;
var allstat3 = [];
var kla = $('#Select1').val();
var dataJSon = { klant: kla }

if (kla) {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:52251/Service1.asmx/Statistic_3',
        data: dataJSon,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        processData: false,
        success: function (msg) {
            response = msg.d;
            for (var i = 0; i < response.Items.length; i++) {
                var j = 0;
                allstat3[i] = [response.Items[i].Interventie, response.Items[i].Sum[j], response.Items[i].Sum[++j], response.Items[i].Sum[++j], response.Items[i].Sum[++j], response.Items[i].Sum[++j]];
            }
            fillDataTable(allstat3);
        },
        error: function (e) {
            alert("error loading statistic 3");
        }
    });
} else {
    alert("statistic 3 null");
}
}

I'm testing too with JSON.stringify({ klant: kla }) and I have the same error.

I looked at several forums but in vain. What's wrong?

Upvotes: 0

Views: 2810

Answers (2)

Syed Fasih
Syed Fasih

Reputation: 312

You need to stringify the parameter sefore sending it to the web service using JSON.stringify() method.

function getStatistic3() {

var response;
var allstat3 = [];
var kla = $('#Select1').val();
**var dataJSon = JSON.stringify({ klant: kla })**

if (kla) {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:52251/Service1.asmx/Statistic_3',
        data: dataJSon,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        processData: false,
        success: function (msg) {
            response = msg.d;
            for (var i = 0; i < response.Items.length; i++) {
                var j = 0;
                allstat3[i] = [response.Items[i].Interventie, response.Items[i].Sum[j], response.Items[i].Sum[++j], response.Items[i].Sum[++j], response.Items[i].Sum[++j], response.Items[i].Sum[++j]];
            }
            fillDataTable(allstat3);
        },
        error: function (e) {
            alert("error loading statistic 3");
        }
    });
} else {
    alert("statistic 3 null");
}
}

Upvotes: 1

Rob Angelier
Rob Angelier

Reputation: 2333

Your webservice method requires a string parameter, but you send the JSON representation of a customer object. I think the build-in JavaScriptSerializer is trying to deserialize your parameter and is causing the error. I adjusted your code in the example below:

function getStatistic3() {

var response;
var allstat3 = [];

    $.ajax({
        type: 'GET',
        url: 'http://localhost:52251/Service1.asmx/Statistic_3',
        data: $('#Select1').val(),
        dataType: 'json',
        processData: false,
        success: function (msg) {
            response = msg.d;
            for (var i = 0; i < response.Items.length; i++) {
                var j = 0;
                allstat3[i] = [response.Items[i].Interventie, response.Items[i].Sum[j], response.Items[i].Sum[++j], response.Items[i].Sum[++j], response.Items[i].Sum[++j], response.Items[i].Sum[++j]];
            }
            fillDataTable(allstat3);
        },
        error: function (e) {
            alert("error loading statistic 3");
        }
    });
}

Webservice method

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public ResponseStatistic_3 Statistic_3(string klant)
{
        Statistic_3[] items = Helper.Helper_Statistic_3(klant).ToArray();
        ResponseStatistic_3 response = new ResponseStatistic_3(items);
        return response;
}

Upvotes: 1

Related Questions