skip to Main Content

First, I’m sending an entire json object, full of data. Following, is only an example of a string data that doesn’t get converted properly.

starting from this string:

"Verticale dall'alto verso il basso, Verticale dal basso verso l'alto"

When sending it with jquery ajax (json), it becomes like this:

"Verticale dall'alto verso il basso, Verticale dal basso verso l'alto"

This is the sending code:

  $.ajax({
    url: ajax_url
    ,type: 'post'
    ,dataType:'JSON'
    ,data: data

Obviously, I don’t want to get escaped single quotes. On my database I want to write “‘”, not “‘”.

How to?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Jintor, the solution was the following:

    url: ajax_url
    ,type: 'post'
    ,dataType:'JSON'
    ,data: JSON.parse(JSON.stringify(data).replace(/'/g, "’"))
    

    In my program, data comes to me from other sources in plain json format, then I have to add some other fields to it, and finally pass it to $.ajax. At that point, what I'm doing is to stringify the original object, replace all the straight quotes, than parse it to JSON again and pass it to ajax.

    Probably I should also take care of double quotes, but anyway, this is my current solution.


  2. you could be smart and use a .replace all quotes to apostrophe

    var str = "she's good";
    var res = str.replace(/'/g, "’");
    

    OR

    You could try var json = encodeURIComponent(string)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search