Access Client Side Variable on Server Side without using hidden field - javascript

I'm trying to store value in client side and access it in server side without using hidden field because of security issues.
I tried using label but I wasn't able to access the value on server side.
I could use web methods but I don't want to postback the page and I cannot use viewstate because its is encrypted and using session is not a good option.

Try doing this, it can be posted to any server side languages such as asp, php or aspx pages.
http://www.w3schools.com/jquery/ajax_post.asp
Jquery Ajax - post huge string value
Thanks,
Anand

Related

how to store json file at server using asp.net as server side language and how to access json data using javascript, html and css?

I am a newbie in web development. I am in the process of building a food blog website. I want to pursue front-end-web-developer career. I have built my website only using client side languages like javascript, jquery, html and css. I have used json to store the data. my questions are -
1. I now want to make my website live. For that I want my website to be hosted on server. As I am not using any server side scripting, would my data related to website be visible to everyone if they do try to "inspect element" or "view source"?
2. how can I separate Json from client side code/scripting? I think for that I will have to seperate json data from other javascript code and store it into a separate file on a server. But in that case how can I access the Json data? For that I will have to make use of some server side scripting language. I prefer Asp.net(not that I know asp.net but I am familiar with C# hence). I also would have to make changes to my html code at client side to fetch the Json data from server. I am not really aware of this whole thing. Could someone spare some time and let me know how to go about it?
3. Does all the client side and server side code lies in a single asp.net project?
I have searched over internet. Mostly all the material/coding available over internet is mostly in php or python hence I am confused. There no definite guideline that I could find as how to handle the data part only using JSON?
to help you answer your 2nd question
2. how can I separate Json from client side code/scripting?
usually server side is in the controller so your HomeController.cs for instance will have an Index action (public ActionResult Index()) that method will basically be called whenever the browser hits the route Home/Index.cshtml
in order to separate these two, you may fetch or construct your Json in the controller and pass it to the ViewBag, from there you can access it in the view.
using $.ajax will help as well to get the json from the view and update the content of a specific part of your page for instance.

Write to server: using text file with HTML5 Jquery/Javascript

I was wondering if it is possible to write to a server and storing it in a text file using Javascript/Jquery, HTML5. The purpose is, I am making a javascript/jquery game and I wanted to store the scores, without using any server-side language (php, asp, etc.)
Thank you so much for the help, your time is much appreciated.
HTML, JS, CSS, all that is client-side. Meaning it runs in the browser and that's that.
PHP & ASP are server-side, meaning the code gets executed on the server and the result gets sent to the client.
You need a method of communication between the client and server. You can send requests using JS (AJAX) but you'll need something on the server to receive and do something with them. For that you need some server-side code.
So no, you can't update data on a server using only client-side technologies. You can, however, use client-side scripts to communicate with a server and tell the server-side code to update data.

Javascript form validation on client side without server side - is it safe?

Supose I have some form with javascript client side validation and no server side validation.
If user disable javascript in his browser there will no be submit button so he can not send me any data without js enabled.
But I do not know is there any way to change my validation instructions from client browser so he could send me untrusted data and make some damage to my database.
Thanks in advance and sorry for my (possibly) obvious question!!!
No. it is not safe. Use server side validation.
For example, even without the browser, I can read your source code. Then simply use CURL to send a post request with malicious data.
Never, ever trust the client.
No, it's not safe. For example, I could just open up the JavaScript debugger in my browser and override your validation.
Anyone who is capable of disabling javascript on their own is probably also capable of making arbitrary POST requests on their own as well. Ok I might be exaggerating but it's still not safe as someone can do the POST requests without using browser, let alone your form, at all.
I would suggest use both client side and server side validation.
Benifit of client side validation is it's fast, we never hit server until user enter correct input. This saves time and avoid user flustration. This is really helpful in large forms.
However demerit is a bad guy can override the client side validation, Hence to avoid such case we should validate in serverside also before processing data.

Insert the value of a JavaScript function to an SQL Server database

Is it possible to add a JavaScript variable in an SQL Server database database (run an SQL query or stored procedure)? If so, how?
With JavaScript only, no you cannot (unless you're using Node.js of course).
The point is, you'll need some server-side code to interact with your database. You can use JavaScript in the browser to make an Ajax call to a server-side script (PHP, Ruby, Python, ASP.NET, Node.js, etc.) that performs the interaction with the database.
You can not access the database from JavaScript. You need to send your data to PHP, Ruby on Rails or ASP.NET or whatever you are using to implement the back end.
You can use Ajax to post to a web service of some sort (or any other server side script) to pass the JavaScript value. The web service would access the database and save it...
Set value of variable in the hidden field and get value of that field at server side when submit form and save it into the database....
You can easily access a database from JavaScript in Node.js.
If you're doing it from the browser, you'll most definitely need some kind of mid-tier service which translates HTTP queries to the relevant database driver (you can write that in any language you want - PHP, JavaScript, ASP.NET, etc.).

How To Assign Value To Session Using Javascript

Work on Asp.Net C# VS08
I would like to assign a value to Session using javascript.
Is it possible?
example: Session["Id"] = document.getElementById("id").value;
The session is a server-side concept; Javascript has no conception of it.
You can make an AJAX service that sets a session value.
However, you should probably use a cookie instead.
You can set cookies in Javascript using this library, among others.
No it is not possible to do that directly with JavaScript on the client-side.
Sessions are handled on the server-side. In general your browser would simply store a cookie which uniquely identifies a session stored on your server. When you change the value of a session variable, you are storing this on the server, and the browser cookie would be unaffected.
You could however use JavaScript to send an XMLHttpRequest to your ASP.NET application, which in turn changes the session variable on the server-side. Since this involves a round-trip to the server, you would have to wait for the response, to confirm that the operation succeeded.
Assigning to a session variable like this isn't possible in JavaScript. Best you can do is write data to a cookie.
http://www.w3schools.com/js/js_cookies.asp
Otherwise if you want to communicate to the server use Ajax.
No it is not possible. Session is server side object. A couple of ways to do that would be to send cookies from JavaScript and use them on the server side to assign a value to a session variable, or to make an AJAX call and set the values on the backend.

Categories

Resources