0

I'm using jquery to send a json string to an ASMX web service. The code behind for the web service method definition looks like this:

[WebMethod(EnableSession = true)]
public string DoMyProcess(string TheDataID, string TheUserID, string TheData) {

The string TheData is a javascript object MyObject that I serialize using

var TheData = JSON.stringify(MyObject);

The jquery part that sends the json looks like this:

var AjaxData = '{"TheDatatID":"' + DatatID + '","TheUserID":"' + UserID + '","TheData":"' + TheData + '"}';

    $.ajax({
      type: "POST",
      url: "../WebServices/MyServices.asmx/DoMyProcess",
      data: AjaxData,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      cache: "false",
      success: RequestSuccess,
      error: RequestError
    });

As it is, I get a 500 error and the debugger doesn't even fire in VS. However, if I replace var TheData = JSON.stringify(MyObject); with

var TheData = "test";

I'm able to get the debugger to break on the first line of the code behind file.

This must be the result of a problem when sending json within json; it should work but it doesn't. What am I missing?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
frenchie
  • 51,731
  • 109
  • 304
  • 510
  • When you get the web method to raise in the debugger by setting `var TheData = "test"` are the other parameters properly populated (on the server-side), like `TheDataID`? – ironsam Feb 26 '12 at 18:15
  • Have you tried calling stringify on AjaxData? `data: JSON.stringify(AjaxData),` – James Hull Feb 26 '12 at 18:36
  • TheData is specified as string not object. Try to declare TheData as object (MyObject?). – Jules Feb 26 '12 at 21:54

1 Answers1

0

At first you can use fiddler for more details about Error 500.

But I think the mistake is at sending JSON in JSON. You must replace character \" to \"

example:

var TheData = JSON.stringify(MyObject).replace(new RegExp('\"', 'g'),'\\"');

Benji86
  • 16