2

I have a webservice in c# and I am calling it from my jquery script.

This is the webservice in c#

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void LoadService2Daily(string fromDate, string toDate, string[] campaigns)

In script I do this:

var selectedCampaigns = $("#campaignDiv input:checkbox:checked").map(function () {
        return $(this).val();
    }).get();
    console.log(selectedCampaigns);


webServiceUrl = 'http://localhost:4025/vmp_webservice.asmx/LoadService2Daily';
$.getJSON(webServiceUrl,
      { fromDate: valFrom, toDate: valTo, campaigns: selectedCampaigns })
       .done(function (result) {

I got error 500 and when I check the response, it is Object reference not set to an instance of an object.

Note

If I removed the string[] array from the web service, it works perfectly, but when I add it, it stoped working. so I am sure it is because the string array thing but I don't know where exactly is the mistake.

The code without the array have been working for 3 years without any problem, but now I am making some editing and I need to pass that array.

This is a screenshot of the console.log to print the selectedCampgains. Notice that, as I showed you in the code, this log is taken before calling the web service

enter image description here

Edit

I noticed that the url of the web service is

Request URL:http://localhost:4025/vmp_webservice.asmx/LoadService2Daily?fromDate=2014-05-25+00%3A00%3A00&toDate=2014-05-25+23%3A59%3A01&campaigns%5B%5D=default&campaigns%5B%5D=Support

as it doesn't contain the string array, right?

Edit3

This is the whole exception that I get

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • 1
    http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Soner Gönül May 25 '14 at 18:22
  • 1
    @SonerGönül it is not a null exception, the main problem is the `500` error. – Marco Dinatsoli May 25 '14 at 18:37
  • Have you set a breakpoint and stepped through the code of the webservice to find out exactly where the error is being raised? – Rory McCrossan May 25 '14 at 18:40
  • @RoryMcCrossan yes I already did that. but no break point executes which indicates that the web service is not called at all. I think that I am calling it wrong. right? – Marco Dinatsoli May 25 '14 at 18:44
  • 1
    There is a lot going on in the selectedCampaigns variable. What is the result of the selectedCampaigns before you pass it into the server side method? Use a browser debugger to take a peek at the info. Consider something like fiddler as well to see what message is being passed to the server – Manuel Hernandez May 25 '14 at 18:45
  • Can you switch to using POST and use Firebug / Chrome dev tools to examine the request being sent to the server? It _shouldn't_ make a difference but arrays are a bit of a tricky one as they're not officially defined in the HTTP spec (IIRC). Also, what browser are you using? – Basic May 25 '14 at 19:20
  • The argument appears in order. Are you able to see the set the breakpoint in your handler?If it is not breaking rebuild and republish. the code in your IDE needs to be the code that is running on your server. – Manuel Hernandez May 25 '14 at 19:22
  • @Basic I already did that though it is get not post. this is the request Request URL:http://localhost:4025/vmp_webservice.asmx/LoadService2Daily?fromDate=2014-05-25+00%3A00%3A00&toDate=2014-05-25+23%3A59%3A01&campaigns%5B%5D=default&campaigns%5B%5D=Support – Marco Dinatsoli May 25 '14 at 19:22
  • @manuelhe I put break point in the first line in the method but it is not executed. i tried clean and restart but nothing works – Marco Dinatsoli May 25 '14 at 19:28
  • You'll have to move up the chain to find a point where you can step through – Manuel Hernandez May 25 '14 at 19:31
  • @manuelhe it is a web service, so you call it as methods – Marco Dinatsoli May 25 '14 at 19:42
  • @MarcoDinatsoli Yeah so the %5B and %5D in `campaigns%5B%5D=Support¬ there are `[]` which is the generally accepted way of doing arrays (`campaigns[]=Value1&campaigns[]=Value2`). For testing purposes, can you please create a POCO model to use instead of raw values in the LoadService2Daily method signature? Basically create a class with properties matching your parameters and use that in the function instead... `public void LoadService2Daily(MyNewPOCOModel Data)` – Basic May 25 '14 at 20:09
  • @Basic No I can't do that because this web service is integrated with many other systems in the company and all they can do is adding the new string array to their requests. – Marco Dinatsoli May 25 '14 at 20:11
  • 2 Reasons for the request, first is debugging - you should not get a 500 error, you should get an object with some null properties which we can examine (and more importantly, examine the model binder for). Secondly, it won't actually change the signature of the method. You'd still pass in exactly the same parameters – Basic May 25 '14 at 20:21
  • @Basic what can I do please? the method is not invoke, the request is correct, the method declaration is correct. can you access my machine using team viewer? – Marco Dinatsoli May 25 '14 at 20:37

1 Answers1

1

The URL which is being generated has square brackets, can you please check whether this will work without square brackets i.e without %5B and %5D(without url encoding)

http://localhost:4025/vmp_webservice.asmx/LoadService2Daily?fromDate=2014-05-25+00%3A00%3A00&toDate=2014-05-25+23%3A59%3A01&campaigns=default&campaigns=Support
Sunil
  • 150
  • 7