I have working on project that using Django.I have render some data as json format with httpresponse in url like this "1.1.1.1:8080/data". and get data with javascript and AJAX in client side, I should mention that this server have no access to intenet and it work in internal network of our company. this work prefect. but another server like 'example.com' want to render my html and i manage it, this work well but the data "1.1.1.1:8080/data" do not load and return error on pinging.
so is there any solution to make access my django app load data on 'example.com'.
best regarads
Related
I am creating a web app where a user can type in /poll/:id in order to retrieve a poll with the given id from my database. Presumably, in order to do this, my Node JS express server would first need to send over the HTML I want the poll to be displayed on, the JavaScript that handles the frontend and then JSON data representing the poll.
I am running into a roadblock because I have no idea how to handle such a request. As a jumping off point, I tried to make my server simply send over the html page in question upon receiving the request (and hence not using express's static server). The code is as follows:
app.get('/poll/:id', function(req, res, next){
res.sendFile('poll.html');
});
However, the page on the user's end does not handle any of the CSS, hence the page is not styled at all upon being sent to the user, nor is the frontend JS supported. What exactly do I have to do in order to ensure all of that is functional in the frontend? Is there an easier approach to this that I am not realizing?
If you want to generate the HTML on the server-side, use a templating engine, ejs for example. There u would be able to retrieve the data from the database and easily render the page according to that data.
On the other hand, if you want to retrieve the data using javascript then just serve the HTML files statically, and then create another route which would only respond with the JSON object retrieved from the database, (which the frontend would request), and then the frontend-javascript would render the application
Context
A feature that I would like to implement is to add a local(client side) database as offline feature for my cordova app using basic html, css, and js. It would be a fallback in case the internet connection is cut off. What I thought was that it would store the request url and request data inside a local JSON file and would have an option later on to be able to resend those failed request if the internet connection is restored.
Question
Is there a method to manipulate (add/create, edit/update, and delete) a JSON file stored inside a client app using pure JS?
What I've done so far is import and read a JSON file inside my app folder using the following code:
fetch("./database.json")
.then((response) => {
return response.json();
})
.then((data) => console.log(data));
(if there's a better way to do it, feel free to give a feedback in the comments)
That works fine and all, but I can't find any method on how to add/edit/delete an object and save the changes to that same JSON file.
According to this post which is half a decade ago, it is not possible without doing it in the backend.
I am using the express module as the basis for my node.js server, and set up a static middleware as follows:
self.app.use(express.static(__dirname));
Within the root folder I have an html file that includes the following url to a php script on my remote server (completely different to the server hosting the node.js application) that returns jsonp data (having converted from xml data from the dataprovider):
var strURL = 'http://example.com/jsonp.php?callback=?&url=http://dataprovider.com/1.4/?arg1=xyz;arg2=abc';
And then a jquery getJSON call to actually get the json data:
$.getJSON(
strURL,
function (jsondata) {
// do some stuff with the json data
}
);
But when I load the html file that is being served from the static node.js folder, no data is returned... the code never reaches the jsondata function.
However, loading the very same html file placed on a "normal" server, the data is fetched just fine, and also if I load the strURL directly, the data is returned OK.
I suspect that this has something to do with cross domain issues, but for the life of me I can't get the page to work within the static node.js server using express. I've tried various solutions out there, but am now thoroughly confused and frustrated!
Any help would be welcome.
Have had cross domain issues with JSON in the past and got round them by using JSONP. There is a good explanation and tutorial at:
http://json-jsonp-tutorial.craic.com/index.html
In my Angular app config I have:
$httpProvider.defaults.headers.common['Custom-Auth-Token'] = ____;
$httpProvider.defaults.headers.common['Custom-Auth-Signature'] = ____;
These headers are necessary in order to make ANY API requests to the Rails backend server.
On my Rails backend, I'm using Sorcery and have access to current_user.authentication_token in my controller/views. I thus need to pass this to the angular config.js.erb. Is this even possible?
Another issue I have is that the auth-token will then be hashed with an app secret (this is only available on the Rails server and shouldn't ever be seen in the JavaScript), which becomes the auth-signature. How do I perform this action on the rails side, then pass this to Angular's config?
You could put this information in data attribute of a HTML tag. From the Angular app in your frontend you just have to fetch this tag with his id and get the data from it.
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.