Welcome Guest, Not a member yet? Register   Sign In
JSON + Prototype + CI = confusion
#1

[eluser]jmanpa[/eluser]
I am trying to send a long JSON string to CI controller method on the backend and extract the data but I am banging my head at how to get it working. I'm trying with a test string "hello" below initially before i try for my loop-created (much longer) string.

Here is the Ajax Request code:

Code:
var hello = "{\"all\":{\"fun\":\"stuff\"}}";
    console.log(hello.isJSON());
    
    myrequest = new Ajax.Request('http://localhost/ts/index.php/mysearch/performSearch/', {
        method: 'post',
        parameters: hello,
        onFailure: function(oXHR, oJson){
            alert('Something went wrong...' + oXHR.statusText); },
        requestHeaders: {Accept: 'application/json'},
        onSuccess: function(transport){
          var json = transport.responseText.evalJSON(true);
          alert('cool');}
        })
}

On the backend I want to load it into a variable and make use of it, but no combination of using the CI Input class and json_decode is producing anything but null or empty objects.

If I wanted to dump all->fun on the server-side to the error_log, what do I need to do with $this->input->post('...') and json_decode to get it working?
#2

[eluser]bretticus[/eluser]
This is probably an error in the way you are setting parameters in Ajax.Request for Prototype.

You might use firebug (or another development tool that shows request headers) to see how Prototype is converting your object array. Furthermore, you can use firephp code (google it) to see PHP's rendition of the $_POST array server-side.

[quote author="jmanpa" date="1282254697"]If I wanted to dump all->fun on the server-side to the error_log, what do I need to do with $this->input->post('...') and json_decode to get it working?[/quote]

Code:
log_message('info', 'post array: ' . var_export($_POST, TRUE));

Make sure logging is enabled for the level you need ('info' in this case.)
#3

[eluser]jmanpa[/eluser]
Thanks for the tip on firephp and using $_POST.

It seems that sending a multi-dimensional hash won't work. It has to be flat. Once I did that, it was fine.
#4

[eluser]bretticus[/eluser]
[quote author="jmanpa" date="1282342775"]Thanks for the tip on firephp and using $_POST.

It seems that sending a multi-dimensional hash won't work. It has to be flat. Once I did that, it was fine.[/quote]

Good to know, thanks.




Theme © iAndrew 2016 - Forum software by © MyBB