Hi,
My .net MVC action is going to respond to ajax requests (jquery).
Do I (or should I) set the document type of my header?
I will be responding with either just html/plain text or maybe even JSON.
Hi,
My .net MVC action is going to respond to ajax requests (jquery).
Do I (or should I) set the document type of my header?
I will be responding with either just html/plain text or maybe even JSON.
If it's responding with html/plain text use:
Content-Type: text/html;
If it's responding with JSON use:
Content-type: application/json
It is good practice to do so, although it really depends on what you're doing with the responded data.
Sometimes JavaScript frameworks will automatically work the response depending on the Header. For example PrototypeJs automatically sanitizes
and eval
s the response if it has "application/json
" header.
I don't know much about MVC actions, but in .NET 3.5, you'd usually use a WPF service (.svc) to respond to a request, and it responds using JSON by default.
It can also accept JSON input using the WebInvoke
annotation's RequestFormat
property.
If you return View() or Json(), as usual, then this is already done for you. No action required on your part.