I am attempting to use AJAX to send a JSON object from the client side to the server. I would like to take the JSON and serialize it as xml and store it in a SQL Server.
I am performing a POST and sending the JSON as a string using :
var Full_JSON = products_json[product_id];
var Message = "";
if (FULL_JSON == null) {
Message = "No Products_JSON!";
}
if (Message.length == 0) {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "/Query.asmx/InsertXMLJSON",
data: "{'JSON':'" + JSON.stringify(Full_JSON) + "'}",
success: function (Record) {
if (Record.d == true) {
console.log("AJAX Success: " + JSON.stringify(Record));
}
else {
console.log("AJAX Fail: " + JSON.stringify(Record));
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus);
console.log("Error: " + errorThrown);
}
})
}
I then take the "JSON" string that is passed and call:
public static bool InsertXMLJSON(string UserEmail, string Time, string Product, string JSON)
{
System.Xml.XmlDocument xmlJSON = JsonConvert.DeserializeXmlNode(JSON, "root");
}
When this is called I get the exception shown below:
After parsing a value an unexpected character was encountered: Q. Path 'Calculations.1.Calculation_Logic', line 1, position 4167.
Here is a snippet of the JSON string where the character is encountered:
\"Calculation_Logic\":\"Math.round((products_json[product_id][\"Questions\"][1][\"Answer\"])/(products_json[product_id][\"Questions\"][3][\"Answer\"]))\"
I have noticed that the escaping of the quotations within the square bracket seems to be part of the issue. If I remove the quotations it parses it just fine.
Any idea why this would be causing issues?
It seems to me that you stringifying simple string variable called Full_JSON. You tried to send it instead of JSON.stringify(Full_JSON)? Without full code sample it is hard to understand what exactly you are stringifying, but it probably the cause. For example:
var str = '{"2003":{"1":{"2":["test"],"3":["test2"]}}}';
var obj = {"2003":{"1":{"2":["test"],"3":["test2"]}}};
//this one looks very similar to your problem
console.log( JSON.stringify(str) ); // {\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}}
//and this one is correct use of this component
console.log( JSON.stringify(obj) ); // {"2003":{"1":{"2":["test"],"3":["test2"]}}}
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.