简体   繁体   中英

Asp.Net MVC Sending JSON As a String Always Truncated on Server

I am sending json data as a string to the server in asp.net mvc as shown in the following;

var dto = JSON.stringify(transObj);
                    console.log(String(dto));
$.ajax({
                    url: "/home/activity",
                    dataType: 'html',
                    data: "obj=" + String(dto) + "&trackedFromEntityTypeId=" + trackedFromEntityTypeId + "&trackedFromEntityType=" + trackedFromEntityType,
                    type: 'POST',
                    success: function (data) {
                        var jsonResult = JSON.parse(data);
                        if (jsonResult.status) {
                            bootbox.alert(jsonResult.message, function () {
                                window.location.href = "/";
                            });
                        } else {
                            displayError('Something bad happend. ' + data.message);
                        }
                    },
                    error: function (error) {
                        displayError('Something bad happend. ' + error.statusText);
                    },
                    complete: function () {
                        $.unblockUI();
                    }

                });

As you can see before it goes to the server, iam writing the string to the console. When i open the console window, the data shown in json format as expected but on the server the string is truncated as shown the following picture; 在此处输入图片说明在此处输入图片说明 However, the orignal json data in console window is shown below (as expted). i also used the jsonformatter to format the data as json and its formating the data properly;

{ "ActivityInformation" : { "Attributes" : [ { "AttributeId" : "18",
            "AttributeMappingId" : "17",
            "AttributeName" : "Number of Helpers",
            "AttributeValues" : [ { "Value" : "" } ]
          },
          { "AttributeId" : "19",
            "AttributeMappingId" : "12",
            "AttributeName" : "Thickness",
            "AttributeValues" : [ { "Value" : "" } ]
          },
          { "AttributeId" : "14",
            "AttributeMappingId" : "16",
            "AttributeName" : "Width of Trench",
            "AttributeValues" : [ { "Value" : "" } ]
          },
          { "AttributeId" : "8",
            "AttributeMappingId" : "13",
            "AttributeName" : "Extra No",
            "AttributeValues" : [ {  } ]
          },
          { "AttributeId" : "10",
            "AttributeMappingId" : "15",
            "AttributeName" : "Dozer PL.Num",
            "AttributeValues" : [  ]
          },
          { "AttributeId" : "7",
            "AttributeMappingId" : "14",
            "AttributeName" : "Shovel Plate No (Company)",
            "AttributeValues" : [ { "Value" : "51741" } ]
          }
        ],
      "Id" : "25",
      "Name" : "Formation Preperation",
      "Tools" : [ { "ToolGroupName" : "Grader  -  MG 118 - 2003 - CAT 14H",
            "Tools" : [ { "ToolAttributes" : [ { "AttributeId" : "19",
                        "AttributeMappingId" : "21",
                        "AttributeName" : "Thickness",
                        "AttributeValues" : [ { "Value" : "" } ]
                      },
                      { "AttributeId" : "17",
                        "AttributeMappingId" : "22",
                        "AttributeName" : "Number of Masons",
                        "AttributeValues" : [ { "Value" : "" } ]
                      },
                      { "AttributeId" : "7",
                        "AttributeMappingId" : "23",
                        "AttributeName" : "Shovel Plate No (Company)",
                        "AttributeValues" : [ { "Value" : "51741" } ]
                      }
                    ] } ]
          },
          { "ToolGroupName" : "Grader  -  MG 119 - 2007 - CAT 14H",
            "Tools" : [ { "ToolAttributes" : [  ] } ]
          },
          { "ToolGroupName" : "Backhoe Loader -  BL 108 - 2007 - CAT 422E",
            "Tools" : [ { "ToolAttributes" : [ { "AttributeId" : "18",
                        "AttributeMappingId" : "19",
                        "AttributeName" : "Number of Helpers",
                        "AttributeValues" : [ { "Value" : "" } ]
                      },
                      { "AttributeId" : "13",
                        "AttributeMappingId" : "20",
                        "AttributeName" : "Width of Work",
                        "AttributeValues" : [ { "Value" : "" } ]
                      }
                    ] } ]
          },
          { "ToolGroupName" : "Backhoe Loader -  BL 109 - 2007 - CAT 422E",
            "Tools" : [ { "ToolAttributes" : [  ] } ]
          },
          { "ToolGroupName" : "Backhoe Loader -  BL 110 - 2007 - CAT 422E",
            "Tools" : [ { "ToolAttributes" : [  ] } ]
          },
          { "ToolGroupName" : "Backhoe Loader -  BL112 - 2006 - CAT0416",
            "Tools" : [ { "ToolAttributes" : [  ] } ]
          }
        ]
    },
  "ActivityListInformation" : { "Id" : "14",
      "Name" : "Road Activities"
    },
  "MilestoneInformation" : { "Id" : "8",
      "Name" : "Milestone 3"
    },
  "PlaceInformation" : { "AreaId" : 4,
      "AreaName" : "Area 1",
      "RoadId" : 6,
      "RoadName" : "108D",
      "ZoneId" : 5,
      "ZoneName" : "Zone 1"
    },
  "ProjectInformation" : { "Id" : "3",
      "Name" : "LEVELLING AND ROADS IN AL BARSHA SOUTH – COMMUNITY 671 & 672"
    },
  "SubActivityInformation" : [ { "Attributes" : [ { "AttributeId" : "12",
              "AttributeMappingId" : "6",
              "AttributeName" : "End Chainage",
              "AttributeValues" : [ { "Value" : "" } ]
            },
            { "AttributeId" : "18",
              "AttributeMappingId" : "7",
              "AttributeName" : "Number of Helpers",
              "AttributeValues" : [ { "Value" : "" } ]
            }
          ],
        "Id" : "1",
        "Name" : "Test Sub Activity",
        "Tools" : [ { "ToolGroupName" : "Grader  -  MG 118 - 2003 - CAT 14H",
              "Tools" : [ { "ToolAttributes" : [ { "AttributeId" : "19",
                          "AttributeMappingId" : "21",
                          "AttributeName" : "Thickness",
                          "AttributeValues" : [ { "Value" : "" } ]
                        },
                        { "AttributeId" : "17",
                          "AttributeMappingId" : "22",
                          "AttributeName" : "Number of Masons",
                          "AttributeValues" : [ { "Value" : "" } ]
                        },
                        { "AttributeId" : "7",
                          "AttributeMappingId" : "23",
                          "AttributeName" : "Shovel Plate No (Company)",
                          "AttributeValues" : [ { "Value" : "51741" } ]
                        }
                      ] } ]
            },
            { "ToolGroupName" : "Backhoe Loader -  BL 108 - 2007 - CAT 422E",
              "Tools" : [ { "ToolAttributes" : [ { "AttributeId" : "18",
                          "AttributeMappingId" : "19",
                          "AttributeName" : "Number of Helpers",
                          "AttributeValues" : [ { "Value" : "" } ]
                        },
                        { "AttributeId" : "13",
                          "AttributeMappingId" : "20",
                          "AttributeName" : "Width of Work",
                          "AttributeValues" : [ { "Value" : "" } ]
                        }
                      ] } ]
            }
          ]
      },
      { "Attributes" : [  ],
        "Id" : "2",
        "Name" : "sdfdsfsdfsdf",
        "Tools" : [ { "ToolGroupName" : "Backhoe Loader -  BL 108 - 2007 - CAT 422E",
              "Tools" : [ { "ToolAttributes" : [ { "AttributeId" : "18",
                          "AttributeMappingId" : "19",
                          "AttributeName" : "Number of Helpers",
                          "AttributeValues" : [ { "Value" : "" } ]
                        },
                        { "AttributeId" : "13",
                          "AttributeMappingId" : "20",
                          "AttributeName" : "Width of Work",
                          "AttributeValues" : [ { "Value" : "" } ]
                        }
                      ] } ]
            },
            { "ToolGroupName" : "Backhoe Loader -  BL 109 - 2007 - CAT 422E",
              "Tools" : [ { "ToolAttributes" : [  ] } ]
            }
          ]
      }
    ]
}

Just figured it out with the help of @Alexei Levenkov comment. The problem was, i was preparing data manually and sending the data to the server using query string format. Howerver, the method i choose is POST. So i just prepare a javascript object and then send it as an object to the server;

var dto = { "obj": JSON.stringify(transObj), "trackedFromEntityTypeId": trackedFromEntityTypeId, "trackedFromEntityType": trackedFromEntityType };
 $.ajax({
                        url: "/home/activity",
                        dataType: 'html',
                        data: dto,
                        type: 'POST',
                        success: function (data) {
                            var jsonResult = JSON.parse(data);
                            if (jsonResult.status) {
                                bootbox.alert(jsonResult.message, function () {
                                    window.location.href = "/";
                                });
                            } else {
                                displayError('Something bad happend. ' + data.message);
                            }
                        },
                        error: function (error) {
                            displayError('Something bad happend. ' + error.statusText);
                        },
                        complete: function () {
                            $.unblockUI();
                        }

                    });

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