Rails removes port from local environment variable? - javascript

I have a very strange problem with my rails 4.1.0 application.
Inside local_env.yml I have a variable declared as:
API_URL: 'http://api.myapp.com:3000'
I use this variable in the javascript file app_ready.js.erb like so:
var apiHost = '<%= ENV["API_URL"] %>'
But for some reason when I call the page using this script and inspect the app_ready.js file I can see the line got converted to var apiHost = 'http://api.myapp.com' without the port :3000. Does anybody know what is happening here?
Edit:
Is there some kind of a server sided cache maybe? Because at some point I might have declared API_URL without the port and changed it later. It can't be in the browser cache, because I've actually cleared it and even tried with different browsers with the same results.

Ok, it was the cache. Had to run rake tmp:clear and restart rails server.

Related

Calling a JS function in Blazor Server on production environment causes reconnect

I want to open a link in a new tab in Blazor server and am using js interop:
await JsRuntime.InvokeVoidAsync("open", url, "blank");
This works on my local machine with the build running using dotnet watch run, but when I deploy it to the server I get a "Attempting to reconnect..." for a few seconds and the JS doesn't run.
Does anyone know what's happening and how I can fix this? Thanks.
EDIT1: Some additional context - this code is being called by ChartJS's OnClick callback for a linechart, which probably means that JS is on the other end of that. I have also found that using NavigationManager.NavigateTo within that method callback causes the same issue.

The Basics needed for creating a map in D3js [duplicate]

I try to import a local .json-file using d3.json().
The file filename.json is stored in the same folder as my html file.
Yet the (json)-parameter is null.
d3.json("filename.json", function(json) {
root = json;
root.x0 = h / 2;
root.y0 = 0;});
. . .
}
My code is basically the same as in this d3.js example
If you're running in a browser, you cannot load local files.
But it's fairly easy to run a dev server, on the commandline, simply cd into the directory with your files, then:
python -m SimpleHTTPServer
(or python -m http.server using python 3)
Now in your browser, go to localhost:3000 (or :8000 or whatever is shown on the commandline).
The following used to work in older versions of d3:
var json = {"my": "json"};
d3.json(json, function(json) {
root = json;
root.x0 = h / 2;
root.y0 = 0;
});
In version d3.v5, you should do it as
d3.json("file.json").then(function(data){ console.log(data)});
Similarly, with csv and other file formats.
You can find more details at https://github.com/d3/d3/blob/master/CHANGES.md
Adding to the previous answers it's simpler to use an HTTP server provided by most Linux/ Mac machines (just by having python installed).
Run the following command in the root of your project
python -m SimpleHTTPServer
Then instead of accessing file://.....index.html open your browser on http://localhost:8080 or the port provided by running the server. This way will make the browser fetch all the files in your project without being blocked.
http://bl.ocks.org/eyaler/10586116
Refer to this code, this is reading from a file and creating a graph.
I also had the same problem, but later I figured out that the problem was in the json file I was using(an extra comma). If you are getting null here try printing the error you are getting, like this may be.
d3.json("filename.json", function(error, graph) {
alert(error)
})
This is working in firefox, in chrome somehow its not printing the error.
Loading a local csv or json file with (d3)js is not safe to do. They prevent you from doing it. There are some solutions to get it working though. The following line basically does not work (csv or json) because it is a local import:
d3.csv("path_to_your_csv", function(data) {console.log(data) });
Solution 1:
Disable the security in your browser
Different browsers have different security setting that you can disable. This solution can work and you can load your files. Disabling is however not advisable. It will make you vulnerable for all kind of threads. On the other hand, who is going to use your software if you tell them to manually disable the security?
Disable the security in Chrome:
--disable-web-security
--allow-file-access-from-files
Solution 2:
Load your csv/json file from a website.
This may seem like a weird solution but it will work. It is an easy fix but can be unpractical though. See here for an example. Check out the page-source. This is the idea:
d3.csv("https://path_to_your_csv", function(data) {console.log(data) });
Solution 3:
Start you own browser, with e.g. Python.
Such a browser does not include all kind of security checks. This may be a solution when you experiment with your code on your own machine. In many cases, this may not be the solution when you have users. This example will serve HTTP on port 8888 unless it is already taken:
python -m http.server 8888
python -m SimpleHTTPServer 8888 &
Open the (Chrome) browser address bar and type the underneath. This will open the index.html. In case you have a different name, type the path to that local HTML page.
localhost:8888
Solution 4:
Use local-host and CORS
You may can use local-host and CORS but the approach is not user-friendly coz setting up this, may not be so straightforward.
Solution 5:
Embed your data in the HTML file
I like this solution the most. Instead of loading your csv, you can write a script that embeds your data directly in the html. This will allow users use their favorite browser, and there are no security issues. This solution may not be so elegant because your html file can grow very hard depending on your data but it will work though. See here for an example. Check out the page-source.
Remove this line:
d3.csv("path_to_your_csv", function(data) { })
Replace with this:
var data =
[
$DATA_COMES_HERE$
]
You can't readily read local files, at least not in Chrome, and possibly not in other browsers either.
The simplest workaround is to simply include your JSON data in your script file and then simply get rid of your d3.json call and keep the code in the callback you pass to it.
Your code would then look like this:
json = { ... };
root = json;
root.x0 = h / 2;
root.y0 = 0;
...
I have used this
d3.json("graph.json", function(error, xyz) {
if (error) throw error;
// the rest of my d3 graph code here
}
so you can refer to your json file by using the variable xyz and graph is the name of my local json file
Use resource as local variable
var filename = {x0:0,y0:0};
//you can change different name for the function than json
d3.json = (x,cb)=>cb.call(null,x);
d3.json(filename, function(json) {
root = json;
root.x0 = h / 2;
root.y0 = 0;});
//...
}

Cordova 6.5.0 fails recording audio using cordova-plugin-media FAILED renaming /storage/emulated/0/tmprecording-1489806941198.3gp

I'm trying to record audio using cordova-plugin-media on Cordova 6.5.0. When I run the method it returns me as 'OK' and calls the success callback function, but when this function tries to get the file, the file doesn't exists.
If I just specify the filename the file is placed at /storage/emulated/0/filename.mp3 but this path is inaccessible to the application. So, when I specify to save using cordova.file.cacheDirectory as reference, it fires an error on the background, but in javascript interface, it still calling the success callback function.
Taking a look at logcat, I found something like this:
E AudioPlayer:
FAILED renaming /storage/emulated/0/tmprecording-1489806941198.3gp to /data/user/0/com.app/cache/recordedData-23-58cca65df12bf.mp3
I've been searching around internet and I found similar errors related to Cordova 3.5.0 as being a BUG but I'm using Cordova 6.5.0, may the BUG still happening?
Also I already checked app's privileges and all needed privileges are already granted. For example:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I've tried using cordova.file.dataDirectory instead cordova.file.cacheDirectory, but I still having no success.
Here goes a piece of the code:
audioCtx = new Media(
cordova.file.cacheDirectory + currentMediaFile,
uploadMediaCapture
);
//console.log("Recording audio.");
audioCtx.startRecord();
Anyone got any clue about how to solve this issue?
Android can not move the file from /storage to /data
The silly part is that moveFile method does not return false if the files can't be moved therefore the Success callback is still called.
So the solution is to replace cordova.file.cacheDirectory with cordova.file.externalCacheDirectory or cordova.file.externalDataDirectory

connect.sid not generated in Bluemix

We have some JavaScript lines working fine on local node.js (and Mongo.db).
But when we moved the code to BlueMix, something does not work.
Let me explain.
I see in the chrome browser there is a cookie named "connect.sid" when all is ok (local server).
And "connect.sid" is not present when code runs in Bluemix, so I can not use "request.session".
Any clues ?
One probable reason could be that due to httpOnly flag is being set. pls. do check on this.

URL path changes between dev and published version

I just got Scott Hanselman's chat app with SignalR working with ASP.NET MVC 4. After hours of configuration, trial and error, and getting different versions of Windows to talk to each other on my home network, it's all working except that I'm left with one issue that I'm not sure how to handle.
This line of javascript has to change, depending on if I'm running the app through Visual Studio or the published (IIS) version:
Works when running within VS:
var connection = $.connection('echo');
Works with published version:
var connection = $.connection('ChatWithSignalR/echo');
When I run within VS, the URL is:
http://localhost:9145/
And the published version is:
http://localhost/ChatWithSignalR
If I don't change that line of code, and try to run the app within VS, using the javascript that has ChatWithSignalR in it, I get an error like this:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:9145/ChatWithSignalR/echo/negotiate?_=1347809290826
What can I do so that I can use the same javascript code and have it work in both scenarios?
var connection = $.connection('??????');
Note, this is in my Global.asax.cs:
RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
This is something you need to take care of because the SignalR library has no idea where the app is deployed to and what its root address is. Something I always do in web applications is have a global Javascript variable called site_root and set it equal to the absolute URL for the root of the site. Now, to do this, you need server tags to evaluate and print that, something like "<%= RootUrl %>" or whatever the syntax is for your server language. Then, when referencing URLs in Javascript, you should always use site_root + "/echo" (with or without the beginning "/" depending on what's printed by the server variable/method). So you'd have something like:
<script type="text/javascript">
var site_root = "<%= RootUrl %>";
// Later, wherever in your code:
function doSomething() {
var echo_url = site_root + "/echo";
// Now you have an absolute URL for the echo page
}
</script>
Now, I put this in the master layout page that always is included - like a Master Page, or depending on what server language you use. Also, instead of a variable like RootUrl, you might use some method to resolve URLs, and just pass it an empty string or "/" to get the root URL for the application.

Categories