A javascript function on a webpage requires data in the following format:
// Javascript
var data = [
{"AKey" : "AVal", "BKey" : "BVal"},
{"AKey" : "AVal", "BKey" : "BVal"},
{"AKey" : "AVal", "BKey" : "BVal"}
];
At the moment, this variable is hardcoded in the javascript file and everything works fine. In the second step, I want to improve it by requesting the data from a server via jQuery's ajax functionality instead of using the hardcoded variable, because of course, until now, the data is static.
So I put a text file on my server, it contains:
// textfile on server
[
{"AKey" : "AVal", "BKey" : "BVal"},
{"AKey" : "AVal", "BKey" : "BVal"},
{"AKey" : "AVal", "BKey" : "BVal"}
];
And apart from that, a PHP script. the PHP script sets its content type to application/json and prints the textfile.
In Javascript, i tried something like this:
var jqxhr = $.getJSON("http://www.myserver.com/output.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
jqxhr.complete(function(){ alert("second complete"); });
Unfortunately, I only get an "error" alert and the two complete alerts.
So I have two questions:
- what's wrong about the ajax call, maybe I should deliver the data in text/plain instead of application/json?
- Apart from that Ajax stuff: as I mentioned above, a specific javascript function requires a variable/data in the format
var data = [ { "A" : "B:}, { "A" : "B:}];
. Is the result of this query (if it would work..) the same format?
The only requirement is that I need both a success and an error handler, just a success handler is not enough.
Thank you