0

I am trying to pass a well-formatted json file as parameter to a method inside a web service. This is the method declaration:

[WebMethod(Description = "Read GDS file", EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void ImportDataGDS(List<GDS_FLCs> FLCs)
{
// code here
}

And here's the GDS_FLCs class definition:

public class GDS_FLCs
{
    public string field1 { get; set; }
    public string field2 { get; set; }
    public int field3 { get; set; }
    public string field4 { get; set; }
    public string field5 { get; set; }
    public string field6 { get; set; }
}

The ajax call in readfile.aspx (as a test, I passed a list composed of only one element):

$(document).on("ready", function () {
    $("button").click(function () {
        var values = { field1: 'Field1_value', field2: 'Field2_value', field3: 1, field4: 'Field4_value', field5: 'Y', field6: '' };
        console.log(JSON.stringify(values));

        $.ajax({
            //contentType: 'application/json',
            data: { FLCs: "[" + JSON.stringify(values) + "]" },
            dataType: 'json',
            type: "POST",
            beforeSend: function () {
                $('#spinner').show();
            },
            complete: function () {
                $('#spinner').hide();
            },
            url: "WebService.asmx/ImportDataGDS",
            success: function () {
                alert("Data import successful");
            },
            error: function () {
                alert("Error: Import failed.");
            }
        });

    });
});

According to Chrome console, this is the data that is being sent to the web service:

FLCs:[{ "field1": "Field1_value", "field2": "Field2_value", "field3": 1, "field4": "Field4_value", "field5": 'Y', "field6": '' }]

However, this is the returned response:

System.InvalidOperationException: ImportDataGDS Web Service method name is not valid. at System.Web.Services.Protocols.HttpServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

As a first step, I would like to get this to work (pass a hardcoded custom list as argument to the web service) and then work on passing the entire file to the web service. I haven't found a way to pass an entire file as argument to the web service using ajax. Is there another way? I checked this link but it didn't help.

Any hints or ideas will be more than welcome. Thanks in advance.

EDIT #1: I changed the declaration of the method to static, but it didn't work:

public static void ImportDataGDS(List<GDS_FLCs> FLCs)
{
// code here
}

(same error message as before)

EDIT #2: Walter's suggestion pointed me in the right direction. I ended up sending the array as string and then deserialized it using the web method.

Now off to the second part of my question, now I need the ajax call read a file and send it to the web method. Here's what I tried:

$.get("test.js", function (data) {
                    $.ajax({
                        data: { FLCs: data },
                        dataType: 'json',
                        type: "POST",
                        beforeSend: function () {
                            $('#spinner').show();
                        },
                        complete: function () {
                            $('#spinner').hide();
                        },
                        url: "WebService.asmx/ImportDataGDS",
                        success: function () {
                            alert("Data import successful");
                        },
                        error: function () {
                            alert("Error: Import failed.");
                        }
                    });
                });

Where test.js is the file that I want to pass to the web service.

At first this seemed to work, but Chrome console (and Firebug as well) indicated that the file had been read by the $.get method (and I could easily see that fact on the console) but was not passed to the web service by $.ajax. I believe I am doing something wrong but can't figure out what.

Community
  • 1
  • 1
gacanepa
  • 323
  • 4
  • 16
  • Shouldn't your webmethod be declared as static? Not quite sure though – cr0ss May 20 '14 at 17:12
  • yes, it should be set to static. – Dean.DePue May 20 '14 at 17:25
  • When you debug the page do you get inside the webmethod at all? – Dean.DePue May 20 '14 at 17:26
  • @Dean.DePue No, I do not get inside the webmethod at all. I also declared the method as static (see Edit #1), with the same result. Any other clues? – gacanepa May 20 '14 at 18:19
  • OK, just try to place a WebMethod call in your page code behind and change the ajax call - does it hit the method? – Dean.DePue May 20 '14 at 18:24
  • try this: `data: JSON.stringify({ FLCs: [values] }),` – Walter Stabosz May 20 '14 at 18:26
  • @WalterStabosz it worked! Your idea pointed me in the right direction. I sent the data to the web service as a string and deserialized it there. The first part of my question is solved. Now I need to work on having the ajax call send a complete json file to the web service. – gacanepa May 21 '14 at 13:47
  • Now I need to send a complete file from the ajax call to the web service. Please see above EDIT #2 to see what I've tried. – gacanepa May 21 '14 at 13:49
  • @Dean.DePue thank you as well for your help with this so far! – gacanepa May 21 '14 at 13:57
  • Is your signature for `ImportDataGDS` still `ImportDataGDS(List FLCs)` or did you change it to `ImportDataGDS(string FLCs)` ? You shouldn't have to read FLCs as a `string`, .NET should just deserialize the JSON as long as it's correctly formatted and matches the `GDS_FLCs` class definition. One thing you might want to try is write a WebMethod with the signature `ImportDataGDS(GDS_FLCs FLC)` and see if you can get .NET to deserialize a single `GDS_FLCs` object. Then try practicing on Lists with something like `ImportNumbers(List numbers)`, then put the two together. – Walter Stabosz May 21 '14 at 14:09
  • @WalterStabosz, yes, I changed the signature to ImportDataGDS(string FLCs) but will certainly give your new suggestion a try and get back here with the results! Any ideas on the second part of the question? – gacanepa May 21 '14 at 14:12

0 Answers0