I have a Google map on my web site which I need its markers to be updated by timer.
My solution is:
Make a SQL query to the server DB.
Get the SqlDataReader.
Pass it to the JS code (which manage the map) by a function (RegisterStartupScript?)
The JS clears all existing markers and set new ones according to the SqlDataReader data.
I don't want the whole page to be refreshed when this is done.
Your opinion?
I wouldn't create data access via javascript code, as this could potentially open your site up to various attacks such as SQL injection and XSS attacks. If you are using ASP.NET MVC, you could use a partial page inside your view that hits an action on a controller every N seconds (enforced using javascript), and returns data via a jsonresult (accessed using your sqldatareader, ideally using a repository to abstract data access).
Related
I have an MVC.NET app which using Knockout.js (+ knockout.mapping) to deal with some cascading dropdowns. The data for these comes from a WebAPI call to an external service. As it happens this service requires an authentication token which expires after 2 hours, so I have the MVC app put the data from the service in a System.Web.Caching.Cache and return it from there unless the token has expired where it will grab it again from the service.
This is working fine.
However when I need to get this to the View, I am currently using the following method, which is to have a property of the ViewModel that I assign the service/Cache data to and then do this in the view:
var model = new ViewModel(#Html.Raw(Json.Encode(Model.ReferenceData)))
ko.applyBindings(model);
where Model.ReferenceData is the data from the service.
again this is working fine, but... the thing is with this, that the page then has all that Json data dumped in it on each request.
I would like to use an external JS file for the ReferenceData as then at least it can be cached by the browser and lessen the weight of the page on future requests.
However, I imagine that the overhead of generating a JS file is not that small, along with – what I really need is it to generate a link to that file that changes in much the same way that the built in MVC bundling of js files works – generating a link with a querystring.
My question is: is there an easy way of doing this?
For sure I can, when the cache is filled that first time, generate a js file and reference that from the View, but as I say getting that to change its link each time it is refreshed – or at least working out whether the data in it has changed and updating it only then is where the problem lies.
Any insight to this would be of great help
Thanks
Nat
Version the JS file (you can keep a GUID in the file it-self).
In Application_Start() get this version ID to a static variable.
In your controller pass this static variable data to ViewBag.
Ref your script with this ID
When you regenerate the file, update the version in file as well as your static variable. Next request from the client get the new version with new key.
Now if you want to update clients on the new version you have to use bi-directional protocol like web sockets or long-polling.
I am building a web application using Google Analytics API, but I am running into some difficulties thinking of an implementation.
I am allowing the user to select their profile from three drop-down menus that are dependent on each other (think country->state->city). I am using AJAX to grab a value from the first drop-down (country), and I am trying to populate the second drop-down from Google Analytics API in an external PHP script.
I am trying to bring the Analytics object ($analytics) over to an external script via a $_SESSION variable from index.php, but I cannot call the $analytics functions I need to populate the second drop-down.
I var_dumped the variable, it initially told me it's an incomplete PHP class, so I serialized it and deserialized between the two classes. However, it still gives me:
Fatal error: Call to a member function listManagementWebproperties() on a non-object
I am wondering if there is a way to do this?
Just to clarify:
In index.php
//create a session analytics object for use in external scripts
$_SESSION['analytics'] = serialize($analytics;) //used for API calls
and the line in an external script throwing the error in my AJAX request, propertyID.php:
$analytics = unserialize($_SESSION['analytics']);
$webProperty = $analytics->management_webproperties->listManagementWebproperties($accountID); //the array used to populate the drop-down
EDIT: Could this be an issue due to the API object?
I suppose it could be related to some transient data that makes the class not serializable.
I think you have to create the Google objects in every page call and serialize in $_SESSION only the data useful to create these instances.
I found a solution/workaround to my problem.
Instead of storing the Google_Service_Analytics() object in the session, you can store the Google_Client() object in the session instead. From there you can declare the $analytics object as you would normally:
$analytics = new Google_Service_Analytics($client);
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!
I need to build a website which will be viewed inside a FileMaker Pro 11 Web Viewer. The URL will be initially set by FileMaker. After which the user may change the content of the page, generating new data. The idea is to retrieve the new data back into FileMaker.
How should the website output data for FileMaker to retrieve? Can FileMaker A) read parameters from the URL after it has changed, or B) read in the entire page as XML, then parse the XML and extract the data (Although I'm hoping for an easier solution than this).
My perspective in this matter is mainly as a web developer.
You can create a button that will "get" the contents of the web view via the GetLayoutObjectAttribute function.
First, give the web view object a name.
Next, create a script that sets a field or variable using:
GetLayoutObjectAttribute(objectName;"source") -- returns the URL of the object
GetLayoutObjectAttribute(objectName;"content") -- returns the current content of the object (html, xml, etc.)
Now create a button which calls the newly created script.
if you're going to parse xml, take a look at http://www.briandunning.com/cf/1 for a function to simplify parsing.
i have stored values in mysql and i want to retrive that value in webpage
but the webpage is creted only using javascript
so how can i use that database's value in javascript
You need to have a server side script in the middle so you can retrieve the value using ajax.
What you're asking for is only possible with a restful nosql database like CouchDB.
You really need to give more information as to what you want to do, but as you implied that the page isn't dynamic the method would be to use ajax to request a dynamic page (written in a sever side langugage) that retrieves the data from the database and outputs it in the required format.
You'll need to either...
a) Have a separate, server-side script which can run the appropriate query, and then pass through the data to the page through an AJAX-type call (jQuery et cetera have functions to simplify the AJAX side of things, if you so desire),
OR
b) Put some server-side code into the page that is evaluated before the page is served and writes the data into the page within something that the Javascript can access it from (i.e. the definition of a variable or some such).