request downloading a file basing on criterias from javascript - javascript

I've got a web application (php/symfony2 - but it doesn't matter here) with heavy AJAX usage and quite a lot of javascript in the client side layer. The interface enables the user to filter the data he wants to display (periods, checkboxes, selects, etc.) and basing on those choice, a POST request goes to the server side for data and JSON is returned through AJAX.
Now, I want to add a functionality to download the data file basing on the criterias/filters that reside in the javascript layer - and I'm not sure how should I do it.
Let's say I've got a <a href="some_action_url"> tag that - if I click - it downloads the file. I know how to do all the server-side stuff. But I don't know how to pass the criteria parameters from javascript to the server-side controller a using the <a> tag.

Related

Retrieving data from browser to JavaScript

I have just started out working with JS and I've managed to post data from a MySQL db to the website using node.js, jade and plain JS.
Now I'm trying to do the other way around, i.e. getting data from the website, to the JS code and then inserting it into the db.
What I'm thinking is simply making a textfield with a button. When I fill the textfield and press the button it is collected by the JS script and the inserted to the DB.
I am however having problems with Jade and the listener and I'm unable to even do a console.log using the listener.
This is what I've got so far in my .jade file.
extends layout
script.
var something = function() {
console.log('something')
}
block content
button(onclick='something()') Click
The website renders nicely, but nothing is printed when I click the button.
If someone could give a hint on how to fetch the data in my .js file that would also be appreciated.
In the context of the WWW there are two places that JavaScript can run.
On the server, e.g. with node.js
On the browser, embedded in a <script> element
Since you want to put the data into a database on the server, you want to go with option 1. So don't use a <script> element.
Use a <form> (you could use client side JS to read the data from the form and send it to the server (Ajax) but that seems overcomplicated for your needs and should be layered on top of a plain HTML solution if you were to go down that route).
form(action="/myendpoint" method="post")
label
| Data
textarea(name="foo")
button Submit
Then you just need to write server side code to retrieve that. The specifics of that will depend on how you are implementing the HTTP server in Node.
The question How do you extract POST data in Node.js? provides some starting points.

How Spring MVC send view result with data to ajax code?

I have a Spring MVC application and I am developing a timed quiz. I am using the Thymeleaf for the formation of the final page that the user sees. I wish I could return the processing done by Thymeleaf (vision / resulting page) to the client side, and also pass the time available for the resolution of the question to a JavaScript code.
In short, the Thymeleaf processes the vision, returning a page with the question and options to choose from, and Spring MVC returns to JavaScript code the amount of time the user has to solve the question.
The biggest problem I have is that I can give something back to JavaScript code with #ResponseBody by a message in JSON, for example. But on the server side, I just can return or a vision, or data, not both at the same time.
I could return the contents of the question, the text and the choices together as JSON response (with time of the question), but then I would be taking the work from Thymeleaf, letting JavaScript code to solve the presentation, mixing in this case, different jobs...
Is there a way to respond JavaScript code using Thymeleaf with these criteria in mind? Allowing processing presentation and return information to the client side?
OBS: If you need some sample code, put a comment and I'll be happy to edit my question (although I think there is no need for it ...).
I am afraid you can only send either one at a time. Each response from web server should be of a specific content type, for view it will be text/html for JSON it is text/json. Though, You can send separate requests for the view as well as for JSON data using ajax.

Download external html content client side, send to django server, send processed data back to client

this is what I want to do. I am a newbie at javascript and django. Please let me know how I can go about doing this:
Client clicks button on web page, javascript downloads html content from external website.
Javascript sends html content (likely a web page) to django server.
Django server executes a specific function on the html content, and returns
a JSON serialized piece of data and
a modified version of the downloaded html content.
Javascript displays 1 and 2 client side.
How do I go about doing this? I suspect I need JQuery AJAX for 1), but I can't seem to get it to work with external html. For 2)-4) I am completely lost. Please help, forgive my newbie-ness.
UPDATE: I don't want to get the html content from the server for a reason. If I do that with a lot of users the site I get content from will block the server from further downloads.
Why not make that all bit shorter:
1) Visitor clicks on button on webpage and your webpage sends address of html page to your django server.
2) Django executes the web request using urrlib2 (for example) and downloads the webpage by itself.
3) Django executes that function on content and returns the a) and b)
4) javascript displays a and b.
So what you need:
1) jQuery ajax to send the url to your server.
2) look up some basic examples for django urllib2 about hot to make web requests. Its pretty simple and easy too.
3) Get http://www.crummy.com/software/BeautifulSoup/bs4/doc/ for parsing html you have downloaded with urllib2. Modifying and parsing html documents is super easy with it. If you look up examples and try them out, you will see it yourself.
4) And then just return the html and/or json using django's own Http methods.
Alan

Javascript or jQuery method to parse ASP.NET MVC Route URL

I'm building an ASP.NET MVC 4 website that needs to be available offline (HTML 5 manifests/caching), so I will be unable to take advantage of server side HTML generation.
Right now, I just have generic/cacheable HTML on the View (.cshtml) and I'm making jQuery AJAX calls on document ready to load the data from the server and using mustache.js to generate the HTML.
Being constrained to doing HTML generation only in the client side, I'm unable to use Url.RouteUrl to generate links and also constrained to having to parse the current URL manually when navigating to a details page, and using the Id (or whatever the parameter is) to make the AJAX call to retrieve the information for the specific record that I need.
Since I'm still using MVC URL Routing (and would like to keep using it) to return the corresponding View (as in, http://localhost:27954/Route/Test2/7?mytext=hellow), I'll need a javascript or jQuery function to both be able to parse URL's and retrieve the value for a given querystring parameter, and ideally another method to generate URLs.
Since I'm obviously not the first person in this situation, I was wondering if anyone had any proven methods already that they could share, or any recommendations.
Thanks!

How far can I go with JavaScript?

I need to do as much as possible on the client side. In more details, I would like to use JavaScript to code an interface (which displays information to the user and which accepts and processes response from the user). I would like to use the web serve just to take a date file from there and then to send a modified data file back. In this respect I would like to know if the following is possible in JavaScript:
Can JavaScript read content of a external web page? In other words, on my local machine I run JavaScript which reads content of a given web page.
Can JavaScript process values filled in a HTML form? In other words, I use HTML and JavaScript to generate an HTML form. User is supposed to fill in the form and press a "Submit" button. Then data should be sent to the original HTML file (not to a web server). Then this data should be processed by JavaScript.
In the very end JavaScript will generate a local data-file and I want to send this file to a PHP web server. Can I do it with JavaScript?
Can I initiate an execution of a local program from JavaScript. To be more specific, the local program is written in Python.
I will appreciate any comments and answers.
It could technically, but can't in reality due to the same origin policy. This applies to both reading and writing external content. The best you can do is load an iframe with a different domain's page in it - but you can't access it programmatically. You can work around this in IE, see Andy E's answer.
Yes for the first part, mmmm not really for the second part - you can submit a form to a HTML page and read GET arguments using Javascript, but it's very limited (recommended maximum size of data around 1024 bytes). You should probably have all the intelligence on one page.
You can generate a file locally for the user to download using Downloadify. Generating a file and uploading it to a server won't be possible without user interaction. Generating data and sending it to a server as POST data should be possible, though.
This is very, very difficult. Due to security restrictions, in most browsers, it's mostly not possible without installing an extension or similar. Your best bet might be Internet Explorer's proprietary scripting languages (WScript, VBScript) in conjuction with the "security zones" model but I doubt whether the execution of local files is possible even there nowadays.
Using Internet Explorer with a local file, you can do some of what you're trying to do:
It's true that pages are limited by the same origin policy (see Pekka's link). But this can be worked around in IE using the WinHttpRequest COM interface.
As Pekka mentioned, the best you can manage is GET requests (using window.location.search). POST request variables are completely unobtainable.
You can use the COM interface for FileSystemObject to read & write local text files.
You can use the WScript.Shell interface's Exec method to execute a local program.
So just about everything you asked is attainable, if you're willing to use Internet Explorer. The COM interfaces will require explicit permission to run (a la the yellow alert bar that appears). You could also look at creating a Windows Desktop Gadget (Vista or Win 7) or a HTML Application (HTA) to achieve your goal.
Failing all that, turn your computer into a real server using XAMPP and write your pages in PHP.
see i got what you want to do
best things is do following
choose a javascript library (eg:jquery,dojo,yui etc), i use jquery.this will decrease some of your load
inspite of saving forms data in in a local file, store them in local variables process them and send them to server (for further processing like adding/updating database etc) using XMLHttp request, and when webservice returns data process that data and update dom.
i am showing you a sample
--this is dom
Name:<input type='text' id='name' />
<a href='javascript:void(0)' onClick='submit()'>Submit Form</a>
<br>
<div id='target'></div>
--this is js
function submit()
{
var _name=$('#name').val();// collect text box's data
//now validate it or do any thing you want
callWebservice(_name,_suc,_err);
//above call service fn has to be created by you where you send this data
//this function automatically do xmlHttprequest etc for you
//you have to create it ur self
}
//call this fn when data is sucessfully returned from server
function _suc(data)
{
//webservice has returned data sucessefully
//data= data from server, may be in this case= "Hello user Name"; (name = filled in input box);
//update this data in target div(manipulate dom with new data);
$('#target').html(data);
}
function _err()
{
//call this fn when error occurs on server
}
// in reality most of the work is done using json. i have shown u the basic idea of how to use js to manipulate dom and call servcies and do rest things. this way we avoid page-reloads and new data is visible to viewer
I would answer saying there's a lot you can do, but then in the comment to the OP, you say "I would like to program a group game."
And so, my answer becomes only do on the client side what you are able and willing to double check on the server side. Never Trust the Client!
And I do not want to do my job twice.
If you are going to do things on the client side, you will have to do it twice, or else be subject to rampant cheating.
We had the same question when we started our project.In the end we moved everything we could on the JS side. Here's our stack:
The backend receives and send JSON data exclusively.We use Erlang, but Python would be the same. It handles the authentication/security and the storage.
The frontend, is in HTML+CSS for visual elements and JS for the logic.A JS template engine converts the JSON into HTML. We've built PURE, but there are plenty of others available. MVC can be an overkill on the browser side, but IMO using a template engine is the least separation you can do.
The response time is amazing. Once the page and the JS/CSS are loaded(fresh or from the cache), only the data cross the network for each request.

Categories