Welcome Guest, Not a member yet? Register   Sign In
post JSON?? (i'm getting error 500) [solved]
#1

[eluser]umbungo[/eluser]
I am trying to post JSON like this;
Code:
$.post('/', {'set': '{"number":"38"}'});
so that i can use it as follows on the php side;
Code:
if($this->input->post('set')){
            $settings_array = json_decode($this->input->post('set'));
            if($settings_array['number'] >= 0 && $settings_array['volume'] < 100){
                //do stuff
            }
        }

However it returns an error 500. How do i fix what i'm doing wrong?
#2

[eluser]LuckyFella73[/eluser]
Try using base_url().'controller_name/method_name'

Code:
$.post('&lt;?php echo base_url().'controller_name/method_name' ;?&gt;', {'set': '{"number":"38"}'});
#3

[eluser]CroNiX[/eluser]
why the extra steps? Why not just directly send number? Whats the advantage of wrapping it deeper in json and then having to decode it just to get a single variable when you can just send that variable?
#4

[eluser]umbungo[/eluser]
..also the first code was jquery/JS, so i can't echo php into it.. (not that it is required, i have posting to '/' working with several other 'posts' on the same page.)

@CroNiX; i'm not sure if that was directed at my original implementation or luckyfella's reply.. as to why *i* am using json, well it won't always just be one key/variable pair, the key won't always be the same, and it just makes for much simpler code in my case.
#5

[eluser]LuckyFella73[/eluser]
Usually (at least in my apps) the parameters send to a controller are dynamic, so I suggested that. Then you implement the js in a view-file and are able to echo the base_url().

If not define var base_url = 'echo base_url()'; in your header (js part) and
use that in your plain js files.

If '/' works as you url - are you sending everything to your default controller index method?
Looks "rare" to me. But if you say it works with other scripts on your page I don't know ..

How does your controller part look like? If it's not the url the problem must be there.
#6

[eluser]umbungo[/eluser]
I posted the relevant part of my controller, the second code section. Yes for this purpose i'm sending everything to the default controller/index.

This is what firebug has to say;
Code:
Parametersapplication/x-www-form-urlencoded
set {"number":"38"}
Source
s e t = % 7 B % 2 2 n u m b e r % 2 2 % 3 A % 2 2 3 8 % 2 2 % 7 D [no spaces.. forum SW converts it otherwise]
"NetworkError: 500 Internal Server Error - http://[ip]/"
To be clear, if i change the data in the post {'set': 'number'), there is no error 500. So i can only assume it must be an error in my JSON syntax, or an encoding issue??

EDIT: the issue was that i had it as an object (using {}), but on the php side was treating it like an array (erroring on a foreach). Solution: change the curly brackets to [].




Theme © iAndrew 2016 - Forum software by © MyBB