post JSON?? (i'm getting error 500) [solved] |
[eluser]umbungo[/eluser]
I am trying to post JSON like this; Code: $.post('/', {'set': '{"number":"38"}'}); Code: if($this->input->post('set')){ However it returns an error 500. How do i fix what i'm doing wrong?
[eluser]LuckyFella73[/eluser]
Try using base_url().'controller_name/method_name' Code: $.post('<?php echo base_url().'controller_name/method_name' ;?>', {'set': '{"number":"38"}'});
[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?
[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.
[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.
[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 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 []. |
Welcome Guest, Not a member yet? Register Sign In |