0

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

alapeno
  • 2,724
  • 6
  • 36
  • 53
  • thank you, the dots were typos I only did on stackoverflow, and I removed the semicolon. anything else? Still doesn't work :( – alapeno Feb 18 '12 at 12:02

2 Answers2

1

JSON is a subset of JavaScript. While this

[
            {"AKey" : "AVal". "BKey" : "BVal"},
            {"AKey" : "AVal". "BKey" : "BVal"},
            {"AKey" : "AVal". "BKey" : "BVal"}
];

may be Javascript, the semicolon means it's not JSON. Also, the periods need to be replaced with commas.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
  • thank you, the dots were typos I only did on stackoverflow, and I removed the semicolon. anything else? Still doesn't work :( – alapeno Feb 18 '12 at 12:02
1

you are accessing it through cross domain and it violates Same origin policy.

follow this link to learn how to overcome this.

see my answer on this question $.ajax call working fine in IE8 and Doesn't work in firefox and chrome browsers

and your JSON should be in following format.

[
   {"AKey" : "AVal", "BKey" : "BVal"},
   {"AKey" : "AVal","BKey" : "BVal"},
   {"AKey" : "AVal", "BKey" : "BVal"}
];
Community
  • 1
  • 1
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • All of the content is located on "myserver.com", so there should not be a problem or am I wrong? apart from that, it works in none of the browsers. – alapeno Feb 18 '12 at 12:03
  • put the url like `/output.php` and format the JSON like above. – Chamika Sandamal Feb 18 '12 at 12:05
  • Ok, I changed it accordingly to your post - but I removed the semicolon, I think it isn't valid JSON. When I load my out.php via http://jsonviewer.stack.hu directly from myserver.com/out.php, the viewer can indeed present the data so I think its valid. Apart from that, it still doesn't work. Is there a way I can find out why jquery displays an error alert? edit: with chrome web inspector I found out that jquery successfully loads the out.php with HTTP 200 and the correct body.. – alapeno Feb 18 '12 at 12:15
  • 1
    Thank you, it works now! It seems there was a caching problem with the json textfile on my server.. – alapeno Feb 18 '12 at 12:23