3

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";
  }
}
3
  • 1
    Try without stringifying: data: markers, ... It looks like your data is already json format with matching up parameters for param1, param2. You don't want a root Markers property. Commented Feb 3, 2017 at 10:39
  • 1
    You can also try using an app such as Postman (getpostman.com) to test your web service API. It is available standalone or as a plugin to browsers. It allows you to test the pure API to send and receive data, so you can be sure that what you send and receive is as expected Commented Feb 3, 2017 at 10:44
  • @Andez Thanks, but without your modification this is what I can see in fiddler: {"Markers":"{param1:1, param2:\"test\""} I assume this part is correct if I change it as you suggest then I get {param1:1, param2:"test"} which is not valid JSON. It might be me that doesn't understand this correct. Can you help me further? Commented Feb 3, 2017 at 10:52

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

in answer above, markers is stringified before sending. in script code var markers is a javascript object not JSON, hence the reason it is stringified before sending. what do you see in fiddler when using the above suggestion?
This must be it. you are the second to indicate this way to solve the problem. But how am I to send a json without a root(markers) and still have a vailid json? I have modified the asmx so that it only has one type string in the signature. And then altering the script to var markers ="[ \"Ford\", \"BMW\", \"Fiat\"]"; But still the same error
Then I see this in fiddler: [ "Ford", "BMW", "Fiat"]. The reply from the webservice is still: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
changing up the question like this is going to make it confusing and more difficult to understand what is the problem. Provide a minimal reproducible example that can be used to reproduce the issue. I under stand the sensitive nature of not wanting to expose internal code but without a clear picture of the issue I doubt you will get proper help with out it.
@LarsHansen I suggest putting break points in your code and debugging where the error is being thrown.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.