June 20, 2013, 11:39am
I thought about the idea to unify the response handling of AJAX requests.
Or are there already improvements implemented?
June 20, 2013, 1:45pm
That really depends on the app. Modal isn’t always what’s desired.
June 20, 2013, 2:09pm
This should be configurable for sure, and merely applies to Exceptions.
But it’s also worth to think about a common return format of AJAX requests where parts could be automatically forwarded to the configured option (modal, alert, user-flash, etc …).
message: "It worked!"
message: "Request failed!"
June 20, 2013, 4:04pm
I use something very similar to what you proposed. But instead of using the HTTP status code (which can be retrieved from the response object itself), I use an explicit success indicator:
data: [1, 7, 9]
message: 'There was an error handling your request. Please try again in a few minutes.',
June 20, 2013, 4:25pm
I think it also would make sense then to have something like
$this->renderAjax($success, $message, $data)
June 20, 2013, 8:36pm
We now have the Response object which supports different output formats, including JSON.
For AJAX requests, we recommend using JSON as the data communication format. By setting the response format to be JSON, you can simply return the data to be sent back as the return value of actions. If there’s any problem, you can throw an exception, and the exception will be automatically turned into a JSON result.
June 20, 2013, 9:12pm
But I searched the sources for “ajax” but haven’t found the code you describe, could you post a link? Thanks in advance.
June 20, 2013, 9:23pm
This is not specific for AJAX. The relevant code is the web\Response and ErrorHandler class.
You may try: Yii::$app->response->format = ‘json’;, and then see how the result and exception will be sent to the browser.
June 20, 2013, 9:57pm
And sorry, I was looking at outdated code in my fork.