AJAX output w/o using echo |
[eluser]flackend[/eluser]
Is there a way to return data to your view (using AJAX) from the controller without using echo? Code: function getsomething() { Code: $.ajax({
[eluser]LuckyFella73[/eluser]
I don't think so. RETURN doesn't work afaik. Why don't you like to use "echo" here? Just curious
[eluser]flackend[/eluser]
My understanding of MVC is that it's bad practice to echo anything from your controller. Plus I want keep all my data exchange as private as possible. And what if you want to return an object like an array?
[eluser]LuckyFella73[/eluser]
Quote:My understanding of MVC is that it’s bad practice to echo anything from your controller.In PHP / MVC context it would be bad practice but thats the way you get values via ajax request. In that case it's not bad practice - at least in my understanding. Quote:Plus I want keep all my data exchange as private as possible.Thats a good point where I dindn't find a good solution either (but didn't do much research yet ). Maybe you can use tokens somehow to prevent direct access to you method from "evil-requests". Quote:And what if you want to return an object like an array?In that case you can return/echo json data or xml. In my experience it's allways good to send an appropriate header - setting charset and/or content type (when returning xml at least). I just had a charset problem in google chrome when returning html without sending a utf-8 header the same run...
[eluser]Aken[/eluser]
I always recommend setting output via the Output class, instead of echoing directly. That way, if you want to turn on settings such as output compression, it won't throw errors.
[eluser]flackend[/eluser]
Before starting this thread, I read in another forum post that the output class isn't as quick as exit(). I'll have to look into output compression. Also... Code: echo 0; // TRUE But as far as I know you can't use exit and the terinary operator: Code: echo $this->data->updateSomething($data) ? 1 : 0;
[eluser]InsiteFX[/eluser]
Code: $test = TRUE;
[eluser]flackend[/eluser]
[quote author="InsiteFX" date="1327701717"] Code: $test = TRUE; Cool, so: Code: ($evaluate) ? exit('It was true!') : exit('False!') Thanks! I didn't realize you could evaluate with parentheses like that.
[eluser]CroNiX[/eluser]
The parenthesis are just there for syntax. They're not needed, but look better as it's easier to see what's going on. If php sees a ? it assumes whatever to the left is an evaluation using the ternary operator. |
Welcome Guest, Not a member yet? Register Sign In |