JSON.stringify()-转义问题 [英] JSON.stringify() - Escaping Issue

查看:872
本文介绍了JSON.stringify()-转义问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将AJAX与JQuery一起使用,以将json发送到API服务器.但是,当我使用JSON.stringify()时,服务器似乎存在转义JSON字符串的问题,而在使用完全相同的代码的另一台服务器上,它的工作却没有任何问题.

I am currently using AJAX with JQuery to send json to an API server. However, there seems to be an issue with a server that is escaping the JSON string when I use JSON.stringify() but on another server when using the exact same code it works without any problems.

这是我在以下位置使用stringify的Javascript对象的示例:

Here is an example of the Javascript object that I am using stringify on:

{"jsonrpc":"2.0","method":"get_contacts","params":["4345ert343t34t34t4e564",
{"campaigns":["AI5D"]}],"id":1} 

我正在此处的示例中工作 https://github .com/GetResponse/DevZone/blob/master/API/examples/javascript_synopsis.html

I am working from the examples here https://github.com/GetResponse/DevZone/blob/master/API/examples/javascript_synopsis.html

在一台服务器上,双引号使用反斜杠进行转义,这导致API服务器以解析错误进行响应,因为这显然是不正确的.在另一台服务器上,转义不存在,API运行正常.两台服务器上都使用完全相同的代码.

On one server the double quotes are being escaped with a backslash which is causing the API server to respond with a parse error as this is obviously incorrect. On a different server the escaping is not present and the API works fine. The exact same code is being used on both servers.

有人知道是什么原因造成的吗?可能是编码问题吗?需要注意的一件事是,在一台服务器上,我必须通过所见即所得编辑器输入JavaScript,但是JavaScript在页面加载时似乎可以正确显示.

Does anybody have any idea what could be causing this? Could it be an encoding issue? One thing to note is that on one server I have to enter the JavaScript via a WYSIWYG editor but the JavaScript appears to display correctly on page load.

如果有人有什么好主意,因为我花了很长时间试图弄清楚这一点.

If anybody has any ideas that would be great as I have spent a long time trying to figure this out.

这是我正在使用的JS代码:

Here is the JS code that I am using:

var api_key = '4345ert343t34t34t4e564';
var api_url = 'http://api2.getresponse.com';            
var CAMPAIGN_ID = 'AI5D';

var data = JSON.stringify({
"jsonrpc"   : "2.0",
"method"    : "get_contacts",
"params"    : [
        api_key,
        {
            "campaigns" : ["AI5D"] 
        }
    ],
"id"        : 1
});

console.log(data);

jQuery.ajax({
    url         : api_url,
    data        : data,
    type        : "POST",
    contentType : "application/json",
    dataType    : "json",
    crossDomain : true,
    async       : true,
    success     : function(response) 
    {                        
        alert(JSON.stringify(response));
        console.log(JSON.stringify(response));
    }

推荐答案

我已经找到了解决问题的方法!

I have discovered the solution to the problem!

似乎标头(v.1.2.4)中的Mootools旧版本引起了与JSON.stringify()的冲突.删除旧的Mootools库可解决此问题.

It appears that an old version of Mootools in the header (v.1.2.4) was causing a conflict with JSON.stringify(). Removing the old Mootools library fixes the issue.

显然,Mootools v1.2.4尝试使用其自身的更改覆盖JSON.stringify(),这些更改是不正确的,从而导致反斜杠转义的问题.在 http://上找到此问题outsourceror.blogspot.co.uk/2011/04/mootools-intrudes-on-native-json-and.html

Apparently Mootools v1.2.4 tries to override JSON.stringify() with it's own alterations which are incorrect and in turn causes the issue with the backslash escaping. This issue was found here http://outsourceror.blogspot.co.uk/2011/04/mootools-intrudes-on-native-json-and.html

将Mootools更新到最新版本也应该解决此问题 http://mootools.net/download

Updating Mootools to the latest version should also fix this http://mootools.net/download

这篇关于JSON.stringify()-转义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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