parse xml using jQuery - javascript

i am parsing Xml using ajax function in jquery in a jsp file
$.ajax({
type: "GET",
url: "sites.xml",
dataType: "xml",
success: function(xml) {
}
});
My problem is my xml file is not in the same path as my jsp file. The jsp path is webProject/webcontent/temlates/store/SearchResult.jsp and the xml path is webProject/webcontent/WEB-INF/config/ampliflex.xml.
What should my url be in the $.ajax({ function?
Please Suggest

I would have thought that the WEB-INF directory would be private by default and so not accessible via an AJAX request. Try moving the XML file to some other part of your project.

Why not simply use:
$.ajax({
type: "GET",
url: "webProject/webcontent/WEB-INF/config/ampliflex.xml",
dataType: "xml",
success: function(xml) {
}
});

Do you see the file if you try accessing it directly with your browser (via localhost i mean)? if your ajax request can see the file so can your browser directly. i would suggest trying to find the correct url in your browser and use that in your ajax request.
Furthermore as mentionned above it is possible the WEB-INF directory is not accessible to client (hence my first question in this comment) so you can also try moving the xml file to a directory you know is accessible.

Related

AJAX request fails .zip file OnceBuilder CMS

I'm building another full stack library but i'm so strugling on one AJAX request, which is so ridiculous. I have just found that, this function is strugling to get file archive but it can gets easly another files ( that can be solution but... i need zip file too) plugin 19 is static because it's just testing and only this file exists
#file_get_contents('https://oncebuilder.com/once/plugins/19/plugin.zip');
So basicly both AJAX function are the same
If i lunch the same function from preview mode file_get_contents gets crazy and return error, but if i lunch it from the listing or via browser its ok
AJAX function are both the same
$.ajax({
type: 'POST',
url: once.path+"/ajax.php?c=plugins&o=item_download&id=19",
contentType: "application/json",
dataType: 'json'
})
.error(function() { console.log("Request Error: item_download"); });

How to get data from local JSON using Ajax?

Hello brilliant people!
I've been sitting hours trying to figure out why I cant load a local JSON file using Ajax. I bet it's some silly typo, of that I totally misunderstand something. I'm using Node.js Express.
The location of the two files:
"trollytrains/views/trips.ejs" (Doing the ajax-request)
"trollytrains/json/allstops.json"
$.ajax(
{
type: "GET",
url: "../json/allstops.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
error: function (msg) {
alert("error");
alert(msg.responseText);
}
});
I have tried altering the URL in all kinds of variations:
./allstops.json
allstops.json
json/allstops
/json/allstops
/json.allstops.json
No matter what I type I get a 404 error.
GET http://localhost:1337/json/allstops.json 404 (Not Found) jquery.min.js:4
Am I entering the wrong URL, or is there some other problem in my code that I haven't detected? I'm very new to JSON and Javascript so I wouln't be surprised.
I'd really appreciate some tips!
Best
/ J
Am I entering the wrong URL, or is there some other problem in my code that I haven't detected?
It depends on whether you can access the URL in the request in your example:
GET http://localhost:1337/json/allstops.json 404 (Not Found) jquery.min.js:4
See if you see the correct JSON when you go to http://localhost:1337/json/allstops.json with the browser. If you get 404 then this JSON is not served at this PATH and the problem can be either your backend failing to serve the JSON or you using the wrong URL.
It's impossible to tell you if you backend code is fine if you didn't include even a single line of the code that actually attempts to serve the JSON in question.
My assumption would be that you're not serving the JSON files.
I assume that you entire app is located in trollytrains directory and you have a json directory inside. To serve what's there you need to use something like this if you're using Express:
app.use('/json', express.static(path.join(__dirname, 'json')));
For more options to serve static files with and without Express, see this answer:
How to serve an image using nodejs
Try this.
$.ajax({
url: '../json/allstops.json',
dataType: 'json',
data:
async: false,
success:function()
{}
});

download a file from url - Javascript ajax

There's a CSV file uploaded to the server that I want to parse using javascript/jquery.
I'm trying to get the file using ajax call but it's always giving me error.:
XMLHttpRequest cannot load
https://1fichier.com/?w5hfqz60tk&_=1474818392318. No
'Access-Control-Allow-Origin' header is present on the requested
resource.
$.ajax({
url:'https://1fichier.com/?w5hfqz60tk',
type: "GET",
dataType: "text",
success: function (data){
parseFile(data);
},
error:function(e){
}
});
I need to run the above code in jsFiddle.. How can I bypass this error?
Or is there any alternative way to download a file?
Update: I just found out that adding url like this: https://crossorigin.me/MY_HTTP(S)_LINK solved my problem but I'm looking for an authentic way.
How can I bypass this error? Or is there any alternative way to
download a file?
You can use $.getJSON(), YQL
var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D'https%3A%2F%2F1fichier.com%2F%3Fw5hfqz60tk'%0A&format=json&callback="
$.getJSON(url, function(data) {
console.log(data.query.results)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Path for jquery ajax for reading a json file

I use this to read a json file stored on my server:
$.ajax({
type:"GET",
url:path,
contentType:"application/json; charset=UTF-8",
dataType:"json"
success:function(response)
{
console.log(response);
//stuff...
}
});
I have tried specifying the path as MyProject/build/web/leaflet/temp/xyz.json and as build/web/leaflet/xyz.json.
In both the cases I get a response of 404.
I have tried with both paths using $.getJSON,I find that the error occurs when the readyState of the XMLHttpRequest is 1.
How should I specify the path for getting the json file?
A relative URI in JS in the browser needs to be relative to the HTML document that is hosting the JavaScript.
Add / before your path and have a try.
ex: /MyProject/build/web/leaflet/temp/xyz.json or /build/web/leaflet/xyz.json

How to process TFL news feeds?

Can anyone suggest an idea of accessing tfl traffic news feeds?. The following is the link to get the url for the data feed.
http://www.tfl.gov.uk/businessandpartners/syndication/16492.aspx
The code I tried is as follows:
Code Sample:
$jq.ajax({
type: 'GET',
url: 'http://www.tfl.gov.uk/tfl/businessandpartners/syndication/feed.aspx?email=***********#****.com&feedId=13',
dataType: "xml",
async: "false",
success: function(xml) {
$jq(xml).find('rr_event').each(function() {
//var title=$jq(this).find('title').text();
alert("success!");
});
}
});​
It looks like the link requires you to have valid credentials to access what I expect to be some sort of xml feed? Also, if you are trying to access this url from a different domain other than www.tfl.gov.uk, you will need to use JSONP to fetch the data otherwise you will run into a security exception (cross-domain error).

Categories

Resources