Grabbing json data with Ajax for Cytoscape - javascript

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);

Related

How to save ajax XML Result to disk

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
});
}

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.

As create json file with data in JavaScript, and send that file for JSONP other domain

I need your help with a big doubt.
Question: As I can create JSON file (with data) from JS code?
The reason is which I need use JSONP, and I wanna take that data from other domain (i.e. http://www.example.com/data/clients.json?callback=?), but this data are storage in a DB (WebSQL) and I need which that data is written in the json file (i.e. clients.json)
Please I need your help because three days ago I try do this, but I don't find yet the answer.
P.S.: Sorry for my english, not is my native language.
EDIT
Sorry my question is confused. I try again but for steps.
I need to write a JSON file with data or records from a DB (WebSQL). Actually my functions are working well (add record, update record, delete), but I need to put that data in a json file because, I'll use the next code for get the data from other domain.
(function($) {
var url = 'http://www.example.com/scripts/clients.json?callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json.sites);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);

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.

jQuery.post is not sending data to the specified URL

I have a mobile application and I have a lot of data that I am putting in to a JSON object to store in localStorage. I need to get this data to PHP to process it. I have chosen to use jQuery.ajax to send the data as a JSON object to PHP. However, when I run the function, it gives a success message, but does not go to the url specified. I have a lot of PHP experience but this is my first JS intensive project.
Here is my JS code:
function sendToPHP() {
jQuery.ajax({
type: "POST",
url: "email.php",
data: { "json" : ATRdataJSON},
success: function(data){
console.log("Data Sent!");
},
});
};
ATRdataJSON is a JSON object that has several JSON objects nested inside.
The URL may not be pointing where you think it's pointing. Try:
function sendToPHP() {
jQuery.ajax({
type: "POST",
url: "/email.php",
data: { "json" : ATRdataJSON},
success: function(data){
console.log("Data Sent!");
},
});
};
i'm afraid you cannot send the json object without stringifying it, it may be sent but as a string [object] try to check it first then you may make sure of the url is absolute to make sure it goes to the right controller.

Categories