How to call C# method from web page Javascript? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My issue is that I need to send a TCP message from a web page. This is not possible for security reasons, so I have written a C# function in a program that is already running as part of my production environment to send the TCP message, and I want to invoke that method via the web page. I basically need to host a web service and call a C# function in that web service from javascript in a web page. How can I do this simply?
I have looked around at solutions like asp.net (my web page is part of a GIANT web client that does not and will not be able to utilize .aspx files), and WCF (which seems like overkill for simply sending a TCP message), but none seem to really nail down my intent of calling a C# function in a project that I have already made, from a web client that is already large relatively unchangeable.
Note that what I really want to do is be able to call C# code in a project I've already made and plan to deploy, so what I'd love to be able to add REST functionality to a method there, but I am also very open to simple/smart solutions outside of my existing project to an extent.
Thanks for any guidance!

You have a lot of options, but I would go for a web service, and my own personal preference is an ASP.NET MVC Web API. I don't know exactly which direction to send you down, but you can start from here and go further. It's possible to write a RESTful Web API using ASP.NET MVC and it's a relatively mature product with a large number of users.
http://www.asp.net/web-api
http://www.asp.net/web-api/overview/older-versions/build-restful-apis-with-aspnet-web-api
As for calling the Web API method, you can use any sort of AJAX call. I usually use jQuery's AJAX methods, myself.

The best way will be direct call ajax to your app through a soap/rest web service.
There are also some alternatives for calling server apps from website:
You have as many options as server side services in the world. The simplest, in my opinion, way is to make an ajax call to a php file which will run a call to your C# program. On the other hand it won't be the best solution in case of performance. You should definitely consider using NodeJS as it is a perfect technology to make a server side 'server' that will handle tasks for server and works well with browser calls (through ajax or socket.io). If your TCP message isn't too complex you can rewrite it in Node, or just call the C# from the Node app.
PHP solution:
Just use a file that will execute your program using for instance http://php.net/manual/en/function.exec.php
NodeJS:
there is a great gist which shows how to send tcp messages:
https://gist.github.com/tedmiston/5935757
Ajax calls:
You can use either pure javascript (http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp) or some libraries like jQuery http://api.jquery.com/jquery.ajax/
Socket.io:
simple socket.io example: What is an example of the simplest possible Socket.io example?
Beware: you need to secure the access, leaving ANY way to make an unauthorized call to a server application is highly insecure.

Related

Question ... I am studying the $ .post () method, however, I only see examples with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Good night guys!
I'm new to programming and now I'm studying the $ .post () method and I have a few questions:
1 ° - Why should I use the $ .post () method?
2 ° - In all the examples I see, they always use the $ .post () method to send something to PHP ... Is it possible to send something other than PHP? If so, could you give me examples using only JQUERY, HTML, mysql and JSON? (that I'm studying)
Note: I'm a beginner.
$.post() is a jQuery method that allows you to send a request to a server (typically) with data that can be processed by the server and sent back.
PHP is a programming language that can run in a server, accept the $.post() request, and process the request.
$.post(), the jQuery method, is used in the "front-end" of web apps. The front-end is the part the user uses. So, a typical example of the use of a $.post() request would be a user on a social media app adding new images to their profile. The image is transformed into data (bytes) and sent from the users computer to the server with the $.post() method.
That's where PHP comes in. PHP will receive the data bytes and save it to the users profile. All the magic of making sure we have the right user and pulling up their info then adding the new image to their profile actually happens here. At the end of it, typically a "success" or "error" response is sent. For reference, when you get a 404 error or a 500 error, that is where the error came from. A server.
The front-end of web applications is very limited. It only has JavaScript, jQuery (which is just a sub-set of JavaScript), HTML5, CSS3, and some altered versions of these.
The back end, the server that handles all the requests and sends all the data back, can be done in a plethora of different languages. PHP is an older language but it is still actively updated and still used in a lot of legacy apps. We now have Node.js, Java, Python, etc. A lot of different languages to write a back end.
Hopefully this clears some things up for you. Please feel free to ask me questions in the comment section or send me a message if you don't have enough reputation to comment yet (don't know the numbers lol).

What are the security trends for client side coding in web development [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
There are a lots of new frameworks, technologies coming up. And it's becoming so hard to follow up all of them. One of the thing that confuses me is client side frameworks. I heard that Angularjs,Backbone,Knockout,jsviews,knockback, SPA... are the most popular right now.But I can't understand how does the security concept applies? If we take an example of querying a table form database it's now possible to make queries from client side database, by specifying table name and fields and etc... So if it works that way, than everyone else can write another query and get all other information. I am pretty sure that I am missing something very important here, and it doesn't click my mind. So please can anybody explain me where can I start learning those primitives.
I really appreciate, and I am really eager to learn but I am searching it wrong way I guess.
Whatever the framework used, the security matter will still the same, and very similar to mobile apps:
which data can you afford to be handled in an untrusted environnement
which treatment can be applied in an untrusted environnement
By "untrusted environnement" I mean the browser itself. You have to understand that any code executed in the browser can be corrupted by a medium/good JS developper.
Data security suffer the same threat: giving access to data from your client means that you do not control anymore who is using it.
Once you've dealt with this simple matter, it became easier to decide what must stay on server side, and what can be deported to client.
That said, there are various ways to make data/algorithm steal more difficult:
Obfuscation that comes with minification
Double data validation (forms for example): both client and server side
Authentication protocols, like OAuth
Binary over webSockets, instead of plain json and ajax call...
The browser sandbox imposes some limitations, but mainly to protect the local computer from damages due to malicious JS code. It does not protect your code nor your data from being seen and manipulated by the user itself.
I am using angular for some of my projects. I haven't used other frameworks , but in angular you usually consume an API to get the data. You don't query your database directly. So, the responsability of securing your data is more in you API (Backend) than in your angular client.
You can use OAUTH, or other security method that you want to make your api safe.

AngularJS vs ServerSide rendering [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
A colleague of mine at work proposed a challenge the other day. The problem: Solve one of our common problems human resource management ie. assigning ppl to projects. The one caveat was we had to do so using a technology that neither of us had used. So we set out building this project using the MEAN stack. Its been a fun learning experience thus far, but it has me wondering.
At what point do we decide that server side MVC rendering trumps client side MVC rendering. Most of the customers we work with already have an instance of IIS running somewhere, so we would using use asp.net MVC, which obviously will do most of the HTML rendering on the server side. Even when using angular in combination with node the HTML rendering is server side.
But if we couple angular with IIS we can do full client side HTML rendering. At what point have others made the decision to use Client vs Server HTML Rendering? Are there concerns of slowness on mobile platforms?
What does the community say are the pros and cons of each scenario?
Thanks for the thoughts!
I've gone with a pure separation between the client and server. My server stack relies on PHP (using PDO and ph-pass) and MySQL for persistence. The major advantage in my view is the complete decoupling of the view/presentation from the server side logic. It is absolutely clear in my application that the PHP generates data and returns it formatted as JSON and accepts JSON formatted parameters but has nothing to do with the display (it just creates data structures that are easily parsed on the front-end).
On the front end I'm using AngularJS, UI Bootstrap (angular wrapper for bootstrap), Google Maps V3 Javascript api (wrapped in an angular directive), and D3js (again wrapped in an angular directive).
The few sites I've worked on haven't really encountered any issues on mobile... even when doing some pretty heavy data processing on the front end... I do all my filtering and some aggregation in the client side code for displaying filterable markers on a map and to draw some charts. Unfortunately the app I'm currently referring to is about a week from being made live so I can't show a link ATM.
Here's my main site though, this one isn't dependent on any PHP so all Angular:
http://www.intellectual-tech.com
You can also check out the portfolio sites and http://www.shanklandfinancial.com I did all of these with just AngularJS and no server side code. The site that does rely on a database but is still WIP is http://www.eat-data.org
The longest delay is still in fetching the initial data which is in the 60kb range after gzipping. On mobile the google maps drags things down quite a bit and CSS animation isn't smooth/fast, but everything is still useable.
Another plus is if at some point I decide AngularJS isn't the way to go, or the client wants a native app, the server side code is all entirely re-useable without modification.
The only down side I see really is if the client wants to export the views I don't really have a good means to do that... I've recently used the PHPExcel library to output the data as XLSX files but in terms of charts etc. nothing in my server code is so fancy and would require overhaul if this was required. That said I don't think this is real problem that having printer friendly pages to be "printed to PDF" can't solve.

advantages of different ways of python javascript (pull) communication [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am into implementing a pull communication between my javascript client and python server. I have wasted my last two weeks struggling with websockets and socketing, but now I decided to give it up.
I am looking for the most convenient way to implement python-javascript communication where the client sends requests to the server, to which the server can answer. I have seen lots of ways, some of which:
ajax
json
socket, etc
I would love to hear your opinion on the advantages of the different possibilities. ( If you also have some great tutorials which I can use to get started, I would really appreciate them. )
It seems you are a little bit confused on definitions and technologies, so let me start of by clarifying some defitintions (rough descriptions, not exact):
JSON: A format to pack data structures (like arrays) into a string and to retrieve the data. It is one the most used formats to submit actual data but is by itself not capable of transmitting data: It is only the data itself
AJAX: The dominant technology for sending requests to a server without reloading the complete page. AJAX works through the HTTP protocol and sends the same headers as would be sent if you would send the same request by visiting the page (Exception: Headers that are special to AJAX, depends on library etc.). By itself AJAX is the technology to submit the data but is not the data itself. However, it can, for example, submit form data encoded in the usual URL encoding. But it can also send JSON data
Sockets: The definition of a socket is very broad and general. Normally, it describes a low layer like TCP (below HTTP in hierarchy) but since you are describing JS + Python I would assume you mean browser - client communication. In that case there is WebSocket. I have not worked with that technology yet but from what I hear it allows arbitrary connections etc. For a normal use case way to advanced.
These are the technologies you mention. Additionally, I would like to mention two more:
XML: Like JSON, but a different format. XML has various advantages and disadvantages. From my point of view the biggest advantage is that it more flexible and on complex structures easier to understand than JSON. But this flexibility comes with the disadvantage that it is harder to learn.
JSONP: Similar to AJAX in that it is used for communication but as the name states, it submits JSON data (I think...). Has the advantage of allowing cross-domain requests (also advanced stuff).
Since your question is not that precise, I will answer it (like assumed above) in the Web context: JavaScript is run from the browser and the server is a web application written in Python.
For this I would recommend a combination of JSON & AJAX: JSON is natively available in both JavaScript and Python and libraries like jQuery offer easy interfaces to data recieved via JSON. I recommend AJAX for the same reason: It's easy to use, integrated in most JavaScript libraries and needs no work on the server side whatsoever: For the server there is just a request to handle.
Example on the server side:
# Request comes in:
data = json.loads(request_body)
# Do calculations
output = json.dumps(out_data)
send_response(output)
On the client side, you could plain JavaScript or jQuery for sending the request and handling the response.

What do I need to know before learning Ajax? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Ajax - Asynchronous JavaScript And XML
What does it include? HTML, JavaScript, XML, jQuery?
What is the best way to start learning Ajax? Should I start from the basics of HTML and JavaScript or base my instruction on a particular language or library?
Ajax is, in short, the process of communicating with a webserver from a page, using JavaScript, without leaving the page.
The key things you need to know for this are:
The JavaScript needed to make the request and handle the response
The server side code needed to receive the request and make the response (unless you are using a service that provides this for you)
The server side of this depends very much on what server side environment you are working with, so there is little useful that is specific that could be said. What can be usually said are what form the responses can take.
JSON is a popular approach for sending structured data.
XML is another way to send structured data, but has been falling out of favour of late since JSON is, arguably, easier to work with.
Chunks of HTML are popular for shoving into pages with innerHTML.
Tiny bits of plain text are useful for simple responses.
As for the client side, there are three common approaches:
XMLHttpRequest: Well supported and flexible.
fetch: A replacement for XHR with a nicer API but more limited browser support.
JSONP: A hack to work around the Same Origin Policy rendered obsolete by the introduction of CORS but which you might stumble across from time to time.
I mentioned the Same Origin Policy above. Normally a script isn't allowed to read data from another domain for security reasons. The CORS standard allows you to work around this.
Now for some resources:
The W3C provides a good guide to JavaScript and other web standards if you need some background.
MDN also has an introduction to JS
MSN has a good guide to the XMLHttpRequest object
Ajaxian has an introduction to JSON-P
jQuery has functions to help with Ajax.
You Need to have knowledge of HTML and Javascript. W3Schools has a Tutorial on Basics which will help You learn. The best way to learn is to put some code and use it. And Moreover now, JQuery ( a javascript library ) , makes learning Ajax more fun and easier. The Website has good documentation and some Sample Ajax code too.
AJAX = Asynchronous JavaScript and XML.
So basically it is javascript. jQuery among other things simplifies your code sending AJAX requests. HTML is markup, not language and is not related to AJAX.
You may start with this tutorial.
You need first to understand Javascript and how to program it.
On my side, when I first started to develop Javascript, my experience was mainly C, C++, Perl and the like.
Due to that background, it quickly occur to me the need in Javascript to be able to query data from the current page (without any redirection) to the web server dynamically. I then discovered the usual key Ajax object XMLHttpRequest.
I would recommend you to use the "regular" Javascript at first, perform some basic dynamic actions, like time display, moving text (...).
Then you could try to implement a simple program that display the clock value from your server. Because XmlHttpRequest perform a dialog between the web server and the client (browser).
For that you need to have access to a web server (eg Apache).
You need to chose what language you will use server side to answer the Xmlhttprequests, e.g. PHP, Perl CGI, etc...
You need to have Apache dispatch the page requests to that PHP ... script.
The script will have to output the result.
Browser-Javascript request
==> Web server (eg PHP)
to Display the clock =
"
Back to browser <==
The the javacript code will get that answer and will have to display that result somewhere.
In terms of Book, Javascript 5 by Flanagan is my first choice.
By actually using it. Is the best way of learning something. ANY thing!

Categories

Resources