相关疑难解决方法(0)

由于JSON中的单引号转义,jQuery.parseJSON抛出"无效的JSON"错误

我正在向我的服务器发出请求jQuery.post(),我的服务器正在返回JSON对象(如{ "var": "value", ... }).但是,如果任何值包含单引号(正确转义\'),jQuery无法解析其他有效的JSON字符串.这是我的意思的一个例子(在Chrome的控制台中完成):

data = "{ \"status\": \"success\", \"newHtml\": \"Hello \\\'x\" }";
eval("x = " + data); // { newHtml: "Hello 'x", status: "success" }

$.parseJSON(data); // Invalid JSON: { "status": "success", "newHtml": "Hello \'x" }
Run Code Online (Sandbox Code Playgroud)

这是正常的吗?有没有办法通过JSON正确传递单个引用?

javascript jquery json

202
推荐指数
3
解决办法
17万
查看次数

jquery ajax发送具有特殊字符的数据

我有一个jquery ajax代码发表评论..

function PostComment()
{

   $.ajax({
         type :"POST",
         url:PageUrl+'Post_LectureComment',
         data:"{Comment:'"+$('#txt_PostComment').val()+"',LectureID:'"+87+"',CategoryID:'"+2+"',Author_Id:'"+ 78+"' }",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success:SuccessHandler ,  
         });

         function SuccessHandler(data)
         {}
}
Run Code Online (Sandbox Code Playgroud)

当我在txt_PostComment中使用'like = durgesh'rao发送数据时,它显示错误

请求有效负载:{评论:'durgesh'rao',LectureID:'1250',CategoryID:'2',Author_Id:'135'}

用什么方式发送数据'???

jquery

5
推荐指数
1
解决办法
7210
查看次数

标签 统计

jquery ×2

javascript ×1

json ×1