Updated main.js

Separated data into new object for request. Added try catch for
converting the data into JSON.
Updated main.js

PRevent further exection if the encode fails.