JavaScript - How to escape quotes in var to pass data through Json

I pass some data through Json to Webservice. My problem is that I am passing html (from tinyMCE input), so var has content using quotes and that gives me problems. I pass the values ​​as follows:

 data: '{ id: "' + news_id + '", title: "' + news_title + '", body: "' + news_body + '" }',

Are there any espace quotes in javascript so i can send html to this news_body var?

thank

+5
source share
4 answers

, , Javascript JSON- (, JSON MooTools JSON.js), . (IE8, FF 3.5+, Opera 10.5+, Safari Chrome) JSON- JSON. JSON JSON, , , . YUI JSON - , .

data: JSON.stringify({
  id: news_id,
  title: news_title,
  body: news_body
}),
+10

replace():

function esc_quot(text)
{
    return text.replace("\"", "\\\"");
}

data: '{ id: "' + esc_quot(news_id) + '", title: "' + esc_quot(news_title) + '", body: "' + esc_quot(news_body) + '" }',
+10

:

function addslashes (str) {
    return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

:

data: '{ id: "' + addslashes(news_id) + '", title: "' + addslashes(news_title) + '", body: "' + addslashes(news_body) + '" }',

Many of these features can be found at http://phpjs.org/functions/index

+2
source

if you are familiar with PHP, you can use some functional form based on PHP

phpjs.org

They created a javascript function that works like functions in a PHP library. You can use the addlashes function here.

0
source

All Articles