JSON.stringify () does not escape the apostrophe

... using JSON2.js and JQUERY

as you can see from the first image, the property of the CustomerReport.Title object has an apostrophe. In the code, you can see that I am calling JSON.stringify () on the reportAsJson line, which still has an unescaped apostrophe.

the error returned by $ .ajax () is {"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. ...

At first, I'm just going to ban the apostrophe from the user, but I thought that JSON.stringify () handled this or do I need to set some parameter ????

thanks

enter image description here

enter image description here

enter image description here

+6
source share
2 answers

You can avoid removing these apostrophes by replacing them with HTML ' , - that one - and later decodes HTML objects either on the client side or on the server side.

+6
source

The following works for me after so many unsuccessful attempts for string and other JSON parsing functions:

 updatedString = string.replace(/('[a-zA-Z0-9\s]+\s*)'(\s*[a-zA-Z0-9\s]+')/g,"$1\\\'$2"); 

Where

  • string = string with an apostrophe in it.
  • updatedString = string with apostrophe issue resolved / escaped
0
source

Source: https://habr.com/ru/post/896883/


All Articles