Lars Hansen
Lars Hansen

Reputation: 153

c# webservice return error

I need some help regarding this. Please be gentle with me, Im not an expert - yet.

The thing is that Im trying to send data from a client (browser) to the server (webservice) via JSON. When I watch the POST data in Fiddler, I can see the JSON I send. This is valid (tested). But my webservice, (when I manually test then I get a return OK), returns the following: "{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}"

Unfortunately its only for internal use so I can't publish the webservice for you.

Can someone explain to me whats wrong?

1.) Javascript code:

<script>
  var markers = "{param1:1, param2:\"test\"}";

    $.ajax({
        type: "POST",
        url: "http://xxx.xxx.xxx.xxx/site/util.asmx/CreateMarkers",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify({ Markers: markers }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
       });
</script>

2.) asmx code

<%@ WebService Language="C#" Class="site.Util" %>
using System;
using System.Web.Services;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;

namespace site{

[WebService(Namespace="http://xxx.xxx.xxx.xxx/site/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Util: WebService
{    
  [WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public string CreateMarkers(int param1, string param2)
  {
    return "OK";
  }
}

Upvotes: 3

Views: 707

Answers (2)

Brobic Vripiat
Brobic Vripiat

Reputation: 326

I had the same problem and solved it with this in web.config:

<customErrors mode="Off" />

After that change, Message, ExceptionType, and Stack trace became properly populated instead of the standard "There was an error processing the request." which gets returned for all exceptions when mode=on and is not very helpful for debugging or reporting.

Upvotes: 0

Nkosi
Nkosi

Reputation: 247521

Try formatting the data sent to match the web service method

<script>
    var markers = {param1:1, param2:"test"}; //<-- format the model

    $.ajax({
        type: "POST",
        url: "http://xxx.xxx.xxx.xxx/site/util.asmx/CreateMarkers",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify(markers),  //<-- just send the markers
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
   });
</script>

Upvotes: 1

Related Questions