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.
Related
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.
I am trying to design a message system for my website, but i can't get my ajax running.
So I make a simpler version of the interaction between the files.
Here is my file both test.php and load.php is under the root folder ( public_html).
i have the ajax function in test.php. load.php is just simply echoing "wow".
$("#test").click(function(){
alert("Clicked.");
$.ajax({
url:"/load.php",
type:"POST",
beforeSend: function(){
alert("testing");
},
success:function(result){
$("#result").html(result);
alert(" sss "+result);
}
}).error(function(){alert("wrong");});
});
Now It works perfectly.
...........how to set relative path ...................
Here is the more complicated design
3 files, all in different folder :
messages.php (under root)
showing all the messages
control.php (root/panels)
a panel will be included in messages.php
load.php (root/functions)
control.php will use ajax to call it then display result in control.php
so when user load in messages.php, it will load control.php then run the ajax call control.php.
I am so confused about how to set up these paths for ajax
( including control.php in messages.php works fine)
Thanks
To understand ajax url, I have two things that should always be remembered.
1. If we put slash at the beginning of ajax url, ajax url pattern will be `hostname/yourFile`
example:
// current url: http://sample.com/users
// ajax code load from users page
$.ajax({
url: '/yourFile.php',
...
});
// ajax url will be: http://sample.com/yourFile.php
2. If we don't use slash at the beginning, ajax url will add to the current url in the browser. example:
// current url: http://sample.com/users
// ajax code load from users page
$.ajax({
url: 'yourFile.php',
...
});
//...ajax url will be http://simple.com/users/yourFile.php
I hope this things can help people who want to use ajax.
Happy coding, thanks.
If the files you're trying to contact are in the root you can use /[file].php so that no matter which page you're on the path will be correct. It sounds like you have a relative path issue. Do you get any errors in the console?
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 am trying to use BusinessObject RESTful API to download a generated (pdf or xls) document.
I am using the following request:
$.ajax({
url: server + "/biprws/raylight/v1/documents/" + documentId,
type: "GET",
contentType: "application/xml",
dataType: "text",
headers: {"X-SAP-LogonToken": token, "Accept": "application/pdf" },
success: function(mypdf) {
// some content to execute
}
});
I receive this data as a response:
%PDF-1.7
%äãÏÒ
5 0 obj
<</Length 6 0 R/Filter/FlateDecode>>
//data
//data
//data
%%EOF
I first assumed that it was a base64 content, so in order to allow the users to download the file, I added these lines in the success function:
var uriContent = "data:application/pdf; base64," + encodeURIComponent(mypdf);
var newWindow=window.open(uriContent, 'generated');
But all I have is an ERR_INVALID_URL, or a failure while opening the generated file when I remove "base64" from the uriContent.
Does anyone have any idea how I could use data response? I went here but it wasn't helful.
Thank you!
. bjorge .
Nothing much can be done from client-side i.e. JavaScript.
The server side coding has to be changed so that a url link is generated (pointing to the pdf file) and sent as part of the response. The user can download the pdf from the url link.
You cannot create file using javascript, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least.
To achieve your functionality, you can implement click event which target to your required file and it will ask about save that file to user.
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.