I have a Django app and within it I'm using a template HTML page that utilizes a Javascript library called Cytoscape (allows you to create graphs with nodes and edges).
I have everything set up with regards to templates and static files (the CSS and basic JS functions are working), however the one thing I'm having issues with is the Ajax functions within my main javascript file. These Ajax functions are responsible for displaying the graphs from JSON files (see code below as to how), and they work fine locally without Django. But as soon as I try to incorporate the server-side implementation, the functions can no longer read these files and I don't know how to fix this.
I've attempted to copy over the JSON files into various directories (but I'm still running into a 404 not found error: "Not found filldata/undefined.json", (where filldata is my app) presumably because this URL has not been set up on the server-side.
function onUpload() {
var filename = $('input[type=file]').val().split('\\').pop();
var layoutPadding = 50;
var aniDur = 500;
var easing = 'linear';
var cy;
if (upload_true) {
var graphP = $.ajax({
url: filename,
type: 'GET',
dataType: 'json'
});
var styleP = $.ajax({
url: './style.cycss', // wine-and-cheese-style.cycss
type: 'GET',
dataType: 'text'
});
} else {
var graphP = $.ajax({
url: './undefined.json',
type: 'GET',
dataType: 'json'
});
// also get style via ajax
var styleP = $.ajax({
url: './style.cycss',
type: 'GET',
dataType: 'text'
});
}
The goal here is to be able to upload JSON files on my web page and link the .cycss (css file specific to Cytoscape) within Django to run my page on the server-side. Any help would be appreciated.
Hey Django is a web server. You cannot access files like ./style.cycss etc. You need to set up a static URL, host your files there and use that URL in your code. you can check here about hosting static files. you need to store your JSON in a similar way and use it with a URL.
Related
well my task is running a static site, No servers at all. pure HTML, and i need to load and read an XML file and update the page with the result.
The task is done and can read the xml file if the file is in the same location, the problem is if the xml file is in a separate folder the ajax flails. seems like the url fails.
$.ajax({
type: "GET",
// working url setting - case - 1
// url: "somexmlfile.xml",
// not working - case - 2
url: "../somepath/somexmlfile.xml",
dataType: "xml",
success: function(xml){
// do something with the returned data
},
error: function() {
// display the error
}
});
Case - 1 is the working solution for me, but i need to place the xml file in a separate place.
Then the case - 2 is the way to get to the file which is getting failed.
any idea,
Actually no domain, no servers, its is pure HTML,
All files are in ex:
D:/myfiles/someFolder/index.html
If i put the file in
D:/myfiles/someFolder/xml/myxml.xml
and set the url as
url: "xml/myxml.xml"
this config is working too,
But i'm trying to place the xml file in
D:/myfiles/xml/myxml.xml and need to read the file using ajax setting the url as
url: "../xml/myxml.xml"
Try to use an absolute url:
www.yourdomain.ext/siteDolder/xmlFolder/xmlfile.xml
Finally the solution was to turn off browser security (strict_origin_policy set to false on about:config) on Firefox settings and it works.
I am developing a wordpress plugin and am having some trouble with the ajax side of things.
What my plugin does is add a form and then validate the inputted data and store the values in a database.
My ajax post is as follows :
$.ajax({
type:"POST",
url: "process.php",
data: datastring,
success: function() {
$('#formwrapper').html("div id='message'></div>");
$('#message').html("<h2>Contact form submitted!</h2>")
.append("<p>We will be in touch soon.</p>").hide().fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
The above code is in a javascript file in a folder called 'js' which is in the root directory of my plugin. The problem is when the data is submitted the ajax post cannot find "process.php". It is stored in the root directory of my plugin folder. I am wondering if there is not maybe a function I am missing which will allow me to find the url of the php file relative to the javascript file without having to set an absolute url path everytime ?
change this:
url: "process.php",
to this:
url: "../plugin/process.php",
As a side note you have missed a < in the ajax success here:
$('#formwrapper').html("<div id='message'></div>");
//---------------------^-----------------------------here you missed '<'
is your folder structure like this?
/
/js/file.js
/plugin/process.php
Because then you could just say
url: "../plugin/process.php",
or
url: "/plugin/process.php",
When you want to access a file in a parent folder. You can use the ../ file to go up one directory.
So to solve this, call:
../process.php
Hi I wanted to know if it is possible to get a secured XML file from an AJAX call.
I have the following code to get the xml file (which works):
function loadXMLFile() {
var filename = 'test.xml';
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: parseXML,
error: Fail
});
}
With secure I mean that people can not get the xml file through their browser.
There is no way to write JavaScript that will tell the user's browser how to get some data without also making that data available to the person who controls the browser.
I'm writing a small ASP.NET MVC site which also includes a WEB API in it that I wrote.
I've configured the project on my local IIS as http://localhost/mysite
On the main page of my site I'm including a js script that I wrote:
<script src="#Url.Content("~/Content/js/home.js")"></script>
on the page ready event of that js I call:
$.ajax({
url: 'api/getdetails',
accepts: 'application/json',
cache: false,
type: 'GET',
success: function (data) {
alert(data);
}
});
when looking with Fidler I see that the page call returns a 404 since it doesn't try to load it to the relative path I'm in (http://localhost/mysite) and it tries to load the root of the server - so the call looks like this http://localhost:80/api/getdetails
when I was writing web forms I used to do ajax calls such as this all the time and it always worked.
what am I missing?
Thanks
What I ended up doing is in my layout html I've added a js var:
var baseUrl = '#Url.Content("~/")';
then on my ajax call I've added that base url:
$.ajax({
url: baseUrl + 'api/getdetails',
accepts: 'application/json',
cache: false,
type: 'GET',
success: function (data) {
alert(data);
}
});
this does the trick no matter how the page looks like. even if I navigate to http://localhost/mysite/home/index
It's probably not the perfect solution, and I definitely think the old webforms way which worked was better - but I guess there are pros and cons to any technology.
Still would be happy to hear if someone has a better solution. for now - this does the trick.
When you navigate to
http://localhost/mysite
The behavior is a little different from
http://localhost/mysite/
Try that to confirm. Without the trailing slash, the "mysite" looks like a document name, not a folder, so relative paths would form from the root of the server.
What you may need to do is pass in the site content URL into your home.js and form absolute paths from it in your code.
I have ASP.NET MVC actions that return JSON data. My client uses jQuery's ajax function to retrieve this data and display it on the screen. I use absolute paths to the controller actions in my JavaScript:
$.ajax({
url: '/Home/Load',
type: 'post',
dataType: 'json'
})
The problem is that in some environments, we'll add an extra virtual directory to the front, so the URL is actually /Path/To/App/Home/Load. I am wondering whether there is a way to write my JavaScript so that it doesn't need updated each time I deploy. I could use relative URLs, like ../Home/Index, but occasionally I move my JavaScript code around. Things get extra tricky when using MVC's bundler, too. It would be nice if the ~ character worked.
Here is how I do:
Add the following lines in your main view (here with the Razor syntax):
<script type="text/javascript">
var appBaseUrl = "#Url.Content("~")";
</script>
Then in Javascript:
$.ajax({
url: appBaseUrl + 'Home/Load',
type: 'post',
dataType: 'json'
})
You could put the URL into an element that is relevant to your ajax request:
<div id="mydiv" data-url="#Url.Action("Load", "Home")">Click me to load ajax request!</div>
Then change your JS code to:
$.ajax({
url: $('mydiv').data('url'), // Could be $(this).data('url') if clicking to load
type: 'post',
dataType: 'json'
})
The advantage of this is that it still uses MVCs routing so any custom routes will still work.