[Catalyst] REST Controller + ajax(jquery) with file upload

David Schmidt davewood at gmx.at
Tue May 18 09:47:24 GMT 2010


Hello everybody,

I am experimenting with Catalyst::Controller::REST and jquery. The
goal is to create a Controller that handles media resources (= upload
a file + text input).

Catalyst::Controller::Resources pretty much does what I want.

Problem: Since I cannot seem to upload a file with a XMLHttpRequest my
Controller needs to handle two kinds of requests
1) typical AJAX requests where only a part of the site is changed.
thus no full document is sent "<html><body>...</body></html>" but only
html fragments or JSON encoded data.
2) entire documents to respond to multipart/form-data POST/PUT requests



Here is a list of actions/paths I want to work with:

GET /media
get a list of all resources (respond with json data)

GET /media/$id
get a resource (respond with json data)

DELETE /media/$id
delete a resource (respond with a status msg, e.g. http status code)

GET /media/new
send form for creating a new resource (<form>...</form>)

POST /media
submit a form, unlike the other requests so far this one expects a
complete document in return. the entire page is reloaded.

GET /media/$id/edit
send form for editing a resource (<form>...</form>)

PUT /media/$id
submit the edit form, unlike. this one expects a complete document in
return like the POST request before



So how do I handle the two kinds of output?
One time I want to respond with html documents generated by my
C::V::TT View (POST, PUT requests) and for ajax requests I'd like to
use the serialization mechanism of C::C::REST.
I accomplished this by setting

MyApp/Controller/Media.pm:
__PACKAGE__->config(
    map => {
        'text/html' => [ 'View', 'MyView' ], # lib/MyApp/View/MyView.pm
    }
);

Otherwise catalyst returns a 415 "Content-Type text/html is not supported" msg.
Now form POST/PUT requests are answered with completely new html pages
while ajax requests with jquery dataType "json" will get the
application/json response provided by C::C::REST


I am asking myself if my approach is practicable.
Any input welcome.

david

ps: I want my websites to be able to work with and without js. How can
I achieve this without increasing the complexity of my code too much.
(I vaguely remember this was really easy in RoR)



More information about the Catalyst mailing list