JavaScript - 如何使用var中的转义引号来通过Json传递数据 [英] JavaScript - How escape quotes in a var to pass data through Json

查看:501
本文介绍了JavaScript - 如何使用var中的转义引号来通过Json传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些数据通过Json传递给Webservice。
我的问题是我传递html(来自tinyMCE输入),所以var的内容使用引号,这给了我一些问题。
我传递的值如下:

I'm passing some data through Json to a Webservice. My problem is that i'm passing html (from a tinyMCE input), so the var has content using quotes and that's giving me problems. I'm passing the values like this:

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

是否有javascript中的espace引号,所以我可以在news_body var中发送html?

Is there anyway to espace quotes in javascript, so i can send html in that news_body var?

谢谢

推荐答案

不使用一次性代码,而是使用Javascript JSON编码器(例如由 MooTools的JSON实用程序 JSON.js ),它将为您处理编码。大浏览器(IE8,FF 3.5 +,Opera 10.5 +,Safari和Chrome)支持JSON编码和解码本地通过JSON对象。编写良好的JSON库在存在时将依赖于本机JSON功能,并在不存在时提供实现。 YUI JSON库就是这样做的。

Rather than using one-off code, go with a Javascript JSON encoder (such as provided by MooTools' JSON utility or JSON.js), which will take care of encoding for you. The big browsers (IE8, FF 3.5+, Opera 10.5+, Safari & Chrome) support JSON encoding and decoding natively via a JSON object. A well-written JSON library will rely on native JSON capabilities when present, and provide an implementation when not. The YUI JSON library is one that does this.

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

这篇关于JavaScript - 如何使用var中的转义引号来通过Json传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆