How to save ajax XML Result to disk - javascript

I feel like I am spending too much time for this.I am getting an xml document from a database with an ajax call .The result is in an XML format ,instead of displaying it, I need to have the user download it.
Is there any way to do this that will work both in IE, FF and Chrome ?
function DownloadXML(XML_ID) {
$.ajax({
url: 'webservices/DownloadXML.svc/' + XML_ID,
method: 'GET',
dataType: "text",
success: function (data) {
//download xml here
},
error: onError
});
}

Related

JQuery stop large XML parse from crashing page

I'm getting XML data via jQuery, but the XML I am using has 5,000+ pieces of data I want to get. When my page tries to get the data after a few seconds my page freezes and shows "This page is being slowed down" and then crashes.
Is there a way I can get the data without the page crashing? Maybe slow the process down so it doesn't handle so many processes at once? Here is the code I'm using to get the XML data:
$(document).ready(function() {
$.ajax({
type: "GET",
url: "mymxl.xml",
dataType: "xml",
success: function(xml) {
$(xml).find("programme").each(function() {
$("xmlcontent").append($(this).find("title").text());
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
});

How to read or download generated pdf via ajax with jquery

im creating a service that will transform html to pdf.
Request with parameter(HTML code to be transfered into PDF) ist send with ajax post.
On server side generating pdf file is no problem.
I was trying to send it back with JSON but i figured out that its not a good idea.
So now im sending it back with no changes - full pdf file as it was generated
...
$pdf->Output('out.pdf', 'I');
And here comes the tricky part.
What can i do with it on client side?
I was trying to show it or download it but i could not figured out how.
$.ajax({
type: 'POST',
url: service_url,
data: out,
success: function (data) {
// WHAT SHOULD I DO WITH "data" IF I WANT TO SHOW IT?
error: function () {
alert("Error");
}
});
Thanks
a wild guess would say that you need to create a PDFObject and then throw it into the Ajax.
HTML
<object id="pdf" data="myfile.pdf" type="application/pdf" width="100%" height="100%">
It appears you don't have a PDF plugin for this browser.
No biggie... you can click here to
download the PDF file.
</object>
and in the Ajax you would have to do something like :
$.ajax({
var pdf = $("#pdf");
type: 'POST',
url: service_url,
data: out,
success: function (data) {
pdf.show();
error: function () {
alert("Error");
}
});

Grabbing json data with Ajax for Cytoscape

I am trying to publish a network with Cytoscape web (or cytoscape.js if possible.) Since my data is quiet huge i prefer to export it from Cytoscape desktop and grab it with ajax in my html. Before the last version of Cytoscape (v.3.1.0) i was able to export the network in .xml format using this:
$.ajax({
type: "GET",
url: "data.xml",
dataType: "xml",
error: function(){
alert("Error loading file");
},
success: function(data){
data = (new XMLSerializer()).serializeToString(data);
});
vis.draw({ network: data });
and it was working just fine.
When I try to do the same thing with .json (either converted from .xml or exported as .cyjs from Cytoscape desktop) it doesn't work. I used a similar code for this:
$.ajax({
type: "GET",
url: "data.json",
dataType: "json",
error: function(){
alert("Error loading file");
},
success: function(data){
}
});
var netwdata = data.elements[0];
vis.draw({ network: netwdata });
Although I do not get a loading error, the network is not plotted now. I have no experience with .json so I am sure I am missing something. Any help or comment is welcome.
I think you didn't write serializer for json data. In simple words you didn't convert json object to string. Like you used for xml:
data = (new XMLSerializer()).serializeToString(data);

Can I get a secured XML file from an AJAX call?

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.

Read Data from text file on server using $.ajax to call a php script

This is my first time posting on this site. I have looked over several of the previous postings related to this topic, but did not find anything that works for me. I am trying to use javascript and jquery $.ajax to call a php script on the server and return the contents of the file. Thus far I am not getting any data back. I am able to update the .txt file on the server using the $.ajax, but could use some help in finding out what I am doing wrong to retrieve it. I do not see any errors being generated from the php script and the events.txt file is not blank. vb.net and c# are my native languages so this is a bit foreign to me.
My js is:
function readText() {
var url = "readdata.php";
var result = "";
$.ajax({
url: url,
type: 'get',
dataType: 'text',
success: function (data) {
result = data;
alert(result);
},
async: false
});
}
and my readdata.php script is:
<?
$file=fopen("events.txt","r");
$read=fread($file,filesize("events.txt"));
fclose($file);
echo $read;
?>
Any advise is welcome. Thanks!
The type in $.ajax should be in capitals
type: 'GET'
function readText() {
var url = "readdata.php";
var result = "";
$.ajax({
url: url,
type: 'GET',
dataType: 'text',
success: function (data) {
result = data;
console.info(result);
},
async: false
});
}
After adding the error: function(){} to the ajax call, I was able to work through this issue.
It turned out that part of the issue was permissions on the server (not able to read from file in the file permissions on the server).
Also I was trying to run locally and I did not have php installed on my local machine.

Categories