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.