tags:

views:

30

answers:

3

I am creating a JSON object dynamically and when I send it via an ajax POST I get Disallowed Key Characters as the response. I know that my object is ok because I can create the SAME EXACT object manually and it sends fine. I tried escape() on all of my strings before adding them to the obj but that did not work either.

Am I missing something?

This is my post

$.ajax({
    type: 'POST',

    url: 'http://localhost/test',
    data: obj,
    dataType : 'JSON',
    success: function(){
        console.log('nice');
    }
});

I am using the same obj as in this post

http://stackoverflow.com/questions/3580260/add-to-json-without-knowing-its-structure

+2  A: 

Your page encoding is probably not matching, it means the response can come with some invalid characters, for example:

ÿ¬{"Result":"A"}

You need to ensure that the encoding you are posting matches the encoding on the other side.

BrunoLM
I just tried to post the JSON to the same page that it is being sent from with now luck, so I don't think this is the issue.
Mike
I just realized that my Keys have spaces in them... Is there any way around that one?
Mike
Did you save your file as UTF-8? I had a problem like that once, try saving with ANSI. If it still doesn't work then I guess it's something else.
BrunoLM
Can you provide the code you have on the `/test` ?
BrunoLM
did you see my comment about my keys having spaces? I guess this should not matter... but if I remove all spaces it works fine. Do I need to resort to XML? I also tested with more random chars like * and they also cause the same error. Very strange.
Mike
A: 

I just realized that my Keys have spaces in them

Yeah... the site you are connecting to is probably running CodeIgniter.

CI has some dumb broken input “cleaning” functionality that will deliberately refuse all form parameters with spaces in (or anything other than the alphanumerics and .-/:).

bobince
you guessed it, is there any way to fix this. I have access to the site I am connecting to...
Mike
Not without hacking out `_clean_input_keys` in CodeIgniter's `libraries/Input.php`. The whole of `_clean_input_data` is questionable really; the anti-XSS is totally bogus, if enabled (hopefully not) and it fails to clean things you might actually want to, like control codes or non-UTF-8 sequences.
bobince
A: 

It turns out that this error was caused by CI's input library. On line 215 you will find _clean_input_keys function which uses preg_match() to disallow certain characters in your keys. So when you send JSON around and php recieves it as an array it can throw an error.

To fix this you can either extend the library or edit the CI core.

Mike