简体   繁体   中英

asp.net web service : sending json within json

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?

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'),'\\\\"');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM