Parse server side enum with javascript [duplicate] - javascript

I try to pass my JS variable into razor, my script fragment:
select: function (event, ui) {
var docID = ui.item.DoctorCode;
#{
string org_code = _unitOfWork.Doctors.GetById("").OrganizationCode;
}
doctorOrgLabel.text('#org_code');
}
In GetById() method i want to pass JS variable docID. I'd appreciate any help!

I try to pass my JS variable into razor
This sentence makes strictly no sense at all.
Razor is a view engine used by the ASP.NET MVC framework running on the server to produce some HTML template.
javascript on the other hand is a client side language running on the client. Once the HTML template is rendered to the client and javascript starts to execute there's no longer such notion as Razor.
If you want to pass some javascript variable to the server you have a couple of options:
make an AJAX call to the server
set the value of this variable in some hidden field inside a form and submit this form
use window.location.href to redirect to the server and passing the variable as query string parameter
store the javascript variable in a cookie which will be sent to the server on subsequent requests

Related

How we pass variable to c# method from JavaScript

I want to pass variable to C# method from JavaScript is it possible ?
I had tried below code code:=
<script type="text/javascript">
$(document).ready(function () {
debugger;
var Query = window.location.search;
var i = "<%=QueryStringModule.Decrypt(Query)%>"
});
</script>
but i am getting the name "Query" doesn't exist in the current context
Please help me in the same
This is not possible. C# is code behind, so runs on the server. Any input is generated before the page is build on screen. So passing values from Javascript to C# is the other way around. Look into ajax to send values to the server so they can be parsed.
You cannot pass JavaScript variables to the server side code in that way. JavaScript is client side, which means that the server side code has already run before JavaScript starts executing. To pass a variable back to the server, you will need to submit the variable to the server. You can use an AJAX request to do this without having to load the page again.

JQuery & jade read render data from Nodejs

I am using Jade framework as frontend with Nodejs & express for backend
so when I am rendering a view with data i can't access these data using JQuery
below my service from Nodejs that renders a view with simple data = x
router.get('/test',function (req,res,next) {
res.render('test',{data : 'x'});
});
so the test page will show without any problem, what am trying to do is to read the rendered data using Jquery methods
footer
.pull-right
| Gentelella - Bootstrap Admin Template by
a(href='https://colorlib.com') Colorlib
.clearfix
// /footer content
// jQuery
script(src='/vendors/jquery/dist/jquery.min.js')
alert(data) // no alert or action happen
but the error said (unknown variable of data)
Moreover, I can read it from jade itself
any suggestions?
thank you
Try using the following
|<script>
!='alert("'+data+'")'
|</script>
The JavaScript you are writing inside your jade framework will not be able to see your server side node.js variables because this client side JavaScript is loaded and executed inside the browser, so you have to pass your variables somehow from the server side to client side then obtain these variables using client side JavaScript.
If you are passing some strings , it is easier for you to use the previous way.
|<script> -> open the JavaScript tag.
!='alert("'+data+'")' -> normal jade string that will be inside our JavaScript tag , that means it should give us JavaScript code after being processed by jade which will make our final text alert("x") because data will be replaced by its value.
|</script> -> close The JavaScript tag
but if you want to pass object there are a ways to do so :
Using AJAX requests.
Use JSON.stringify(data) in server side to write your object to a hidden div and in client side using var data = JSON.parse(jQuery('div').text())

How to pass javascript variables into rails controller?

how to pass the date variable into erb puts command
function myDayClickHandler(eventObj){
var date = eventObj.data.calDayDate;
<%puts "asdf",date%>
};
Javascript and Ruby are something very different. Javascript is client-side, running in the browser. Ruby is run on the server side, separately from client-side code.
Ruby code (<% .. %> parts in your example) is run on the server.
Javascript code (var date = eventObj.data.calDayDate;) is executed in the browser. It's also executed after the Ruby code, when server has processed request and generated result page already.
If you want to know value of date on the server, you'll have to send it from the browser to the server in AJAX request. Any popular Javascript library will help with that.
To pass data to the Rails controller from Javascript, you will need to pass the data back through attaching query parameters in the url.
http://www.google.com?variable1=1&variable2=2
Then in the rails controller you access the variable using
params[:variable1] or params[:variable2]
You can use custom controller actions to perform an activity either using javascript to submit or render, or use traditional HTML. Javascript would be preferred for user experience.

How to pass data to jsp and open it in new window from servlet

This is my Problem: I have a jsp with a table using datatables. It displays some users with editing buttons at the end of the table. If you hit the "edit" button, it calls a javascript function getting the userID from the table and pass it via ajax $post. method to a servlet.
The servlet receives data from database via hibernate and maps it into objects.
My code works until this point. Now i want to pass the objects to a jsp and open it in a new window ( a popup or something with a form inside for setting them with data from objects to edit the called user).
Can i do it from the servlet? Or do i have to do it from ajax when
the call succeed?
There are are two approaches to do this: Client Side Rendering or Server Side Rendering:
1.Server Side
You don't need that servlet, what you need is to make your second JSP file for input form. And in your Javascript, open a new window with Form entry JSP page address and add userId parameter to the query string of that page:
function onRowClick(userId){
//Open new window with the address: 'formEntry.jsp?userId=' + userId
}
And in the formEntry.jsp you can get this ID from request params:
String userId = request.getParameter("userId");
Then you can use this userID to retrieve your user object:
//get user from dao or service objects
User myUser = someUserService.findUserById(userId);
And then in your JSP page you can create your input fields like this:
<input name="firstName" type="String" value="<%= myUser.getFirstName %>"
About using java code in jsp (comment):
You are write about not writing java code in JSP pages, but for that purpose you have 2 options:
Use some Server side MVC framework like JSF to separate view from your model and controller
Use pure JSP but don't use lower level layers in JSP, only Service Layer and also better to have some taglibs to decrease required java code inside JSPs
2.Client Side
The second way is to use more javascript to handle the task.
You can go with your servlet way and have your servlet return JSON String of User object. In javascript, you get the json and parse it and put its fields inside html tags:
function ajaxRequestSuccessHandler(data){
var user = //parse data and make a javascript object
document.getElementById('firstName').value = user.firstName;
....
//load all of data in your form.
}
and then you can submit your form to a servlet and save updated data.
Note that this approach is good for 1 or 2 forms, but if you are developing larger softwares, you need to use some more tools and frameworks:
1.It's better to use JAX-RS or Spring-MVC to expose RESTful services and use theme in javascript instead of doing this with pure servlets.
2.Better to utilize some client side MV* framework like angularJS or ReactJS.

How might I pass a variable's value from a Django template into client-side JavaScript?

How can I pass a variable value from a template using JavaScript without using a form (GET OR POST)?
You can send this value from JavaScript via Xml Http Request without using form - see how AJAX works.
See for example POST request with jQuery JS library.
The uri parameter is URI of your Django view, data - JS object that you send to server and finally callback - JS function to process server response.

Categories