1

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.

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} 

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

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.

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.

EDIT:

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));
    }
Sergio
  • 28,539
  • 11
  • 85
  • 132
Daniel West
  • 1,808
  • 2
  • 24
  • 34
  • 1
    How does the json look in the http request sent to the server? If you are using chrome, you can see this in the network tab. – pax162 Nov 04 '13 at 18:35
  • 3
    Are you 100% sure that you're not applying `JSON.stringify()` **twice**? – Pointy Nov 04 '13 at 18:36
  • @Pointy I am definitely only applying stringify once. Please the the updated post above. – Daniel West Nov 04 '13 at 18:39
  • 1
    What gives `console.log(typeof response)`? – plalx Nov 04 '13 at 18:40
  • @pax162 Backslashes are being added to every double quote ("). – Daniel West Nov 04 '13 at 18:41
  • 1
    you just need to stringify `params` (which is an array...) data you send should be an object not a string; then the response you send should be a json as well & not a string – mikakun Nov 04 '13 at 18:42
  • @plalx an object is returned telling me that there is a parse error. Just a generic message. – Daniel West Nov 04 '13 at 18:44
  • 1
    @DanielWest Have you tried not serializing the `data` like suggested by @mikakun? – plalx Nov 04 '13 at 18:45
  • @mikakun Can you give me an example? Why would it work on one server and not another server? – Daniel West Nov 04 '13 at 18:46
  • @plalx I will try to serialize the data but I don't think it will fix the problem. It is not expecting serialized data. – Daniel West Nov 04 '13 at 18:48
  • 1
    @DanielWest You are already serializing the `data`. What I am saying is that you probably shouldn't. – plalx Nov 04 '13 at 18:49
  • 1
    http://php.net/manual/en/security.magicquotes.disabling.php – mikakun Nov 04 '13 at 18:49
  • @plalx Yeah I have removed the JSON.stringify() function from the code and this broke it further. But what I'm saying is that the code is working on one server but as soon as it is moved to another server it just stops working and I get the escaping issues. – Daniel West Nov 04 '13 at 18:50
  • 1
    @DanielWest That's kinda odd, I agree. There must be some environment differences (e.g. server settings / caching might be involved?) and your assertion that the code is identical could also be wrong. It's hard for us to help you with that kind of issue. – plalx Nov 04 '13 at 18:52
  • @mikakun Yeah I thought that it might be due to magic quotes, seems to be the only thing server related that could be causing it. – Daniel West Nov 04 '13 at 18:52
  • @plax I know what you mean but I literally copied and pasted the code and haven't changed anything so there's no reason why it should be any different. – Daniel West Nov 04 '13 at 18:54
  • @mikakun I just tried the code with magic quotes turned on and it still worked without problems. – Daniel West Nov 04 '13 at 19:20
  • From looking into the code a bit more it looks like it might be a conflict with mootools. – Daniel West Nov 04 '13 at 21:51

1 Answers1

3

I have discovered the solution to the problem!

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.

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

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

Daniel West
  • 1,808
  • 2
  • 24
  • 34