views:

44

answers:

5

Hi,

Let us say I have a restful web service which can deal with DTOs in json format to perform a CRUD operation.

Let us also say I use jquery in an unobtrusive way to serialise my form at the frontend using:

JSON.stringify

What can I do to ensure that everything works even if JavaScript is switched off?

Thanks.

Best wishes,

Christian

A: 

Have an alternative version that can be gracefully degraded to. JSON cannot be handled without JavaScript (JSON = JavaScript Object Notation)

Dustin Laine
+1  A: 

I don't see how you can run jquery JavaScript when JavaScript is turned off.

Can you have a version of your app that submits the form to the server and have the JSON serialisation done on the server side?

djna
Exactly - you will need something dynamic to replace the serialization logic. With javascript turned off this has to be done server side.
DanSingerman
Oh but what about flash! couldn't it be done with Flash, or Silverlight or one of the other great non-standard javascript methods??
Anthony
Ok yes. I wonder what the proportion of browsers with flash, but without javascript is? My guess: vanishingly small. (Hell, why not suggest VBscript?)
DanSingerman
Coldfusion! But seriously... Do you think the issue is more with browsers with javascript than user's disabling js? I posted a comment on another answer asking a related question. What is real concern we have (other than our cool apps breaking?) is it browsers without javascript or users with javascript turned off? Cuz i'd put money down that even if a user disabled javascript, they'd leave flash running (not that I think it's a real alternative for the SO's question).
Anthony
I'd say it is more about accessibility - i.e. screen readers, mobile browsers, text only browsers, than it is about users who choose to turn javascript off - another minority who I imagine is extremely small.
DanSingerman
I like this idea the best. HTML forms are still posted in the standard way. This post is transfered into JSON on the server and send to the actual 'service' that does some crud operation. this service could also be cosumed by non humans. thanks!
csetzkorn
A: 

It really depends on what your server-side script is written in, I suppose. Most web app languages have some kind of json support. So if you want your form data to be in json even after it hits the server, use whatever your language supports. In PHP, you could do:

$form_data_json = json_encode($_POST);

Of course that would assume you wanted all of the data passed from the form, which you might not. But that's a simple one-line solution.

Anthony
A: 

The only way to ensure that it works, even with javascript turned off, is to post the information without AJAX and validate and transform all the information on the server. JQuery is JavaScript, so if JS is turned off on the client, it won't work. Most modern server side languages can natively handle serializing/de-serializing JSON, and if not there are sure to be libraries out there to do it for your language of choice.

Kevin
There is an interesting question hinted at by this one: how many users still disable javascript? Isn't it more likely that the user is on an older browser that doesn't have full javascript support than it is to have a user on a modern browser with js turned off? Really over-reliance on javascript (like over-reliance on images, tables, or Flash) should be more a concern for SEO and accessibility, as it's also more likely a blind person will visit your site than someone with js enabled. Or am I way off in this assumption? Is JS turned off more than I think?
Anthony
Actually, it pops up more than you'd think. There are readers that visually impaired people use which have difficulty with JavaScript, and a number of security people recommend turning off JavaScript for any site that you don't explicitly trust. Now I don't do this, but to say that everyone has JavaScript turned on is false.
Kevin
Well I mentioned blind users specifically because I know the slow-pace software like JAWS has made toward javascript support. But it is getting there. But for the "average" user, meaning 75% of web users, how many have javascript disabled on their home computers? I'm still willing to hear that I'm wrong, btw. But I am the front-line of tech support, including security consulting. I tell people "don't eat stuff on the sidewalks" but never "disable js" or "turn off cookies" because realistically the sites the trust (coughfacebookcough) are just as likely, if not more so, to XSS attacks.
Anthony
And by all that I mean, in my experience, people turn off the security features that make the web less fun. Going back to the SO's question, I wonder how many user's are aware that they use jquery on every other page they visit. It's so seamless when done right that most folks still think of pop-ups and alerts that tell them to click "No' if they want to stay on the page, not the slider bar that they use to add to their shopping cart (or whatever). Sorry, I may be begging my own question.
Anthony
A: 

You don't mention what technologies are you using on the server, so I'll try to make this server-agnostic.

You have to point your form action to a different url than your web service (in rails this would be some other "controller action").

<form id="input_form" action="your/non-SOA/action">
  <input type="text" name="user" />
  <input type="submit" value="Submit"/>
</form>

Somewhere else (probably on a js file loaded on the head of the html) you should have a javascript code that modifies how the submit "click" event is handled:

$(function(){ // jQuery DOM ready function. 

   var form = $("#input_form");
   var submit_button = $("#input_form .input[type=submit]");

   submit_button.bind('click', function() {
     // serialize form with JSON and send it here
   });
});

This code will execute only if javascript is enabled. If it isn't, then the form will be sent to the your/non-SOA/action via a POST request, usually in form of 'associative hash' on the action params. So you have to transform this hash into a json structure on the server, and send it to the corresponding web service.

It will look like this in your server (pseudo-code):

function your.non-SOA.action(params)
  // you might need more treatment than just doing to_json, depending on your ws syntax
  serialized_text = params['input_form'].to_json 
  myWebservice = Webservice.new('your-webservice-uri')
  try
    myWebservice.send(serialized_text)
  catch(exception e)
    // handle exceptions
  end
end
egarcia