Good day. I work with transferring JSON array. Does not work. There is such a code that loads the data from the data2.json file:

$.getJSON( "data2.json", function( json ) { console.log( "JSON Data: " + json); }); 

And the data2.json file:

 [ ['Shanghai', 23.7], ['Lagos', 16,1], ['Instanbul', 14.2] ] 

When the page is displayed in the browser, errors are displayed in the console: No 'Access-Control-Allow-Origin' header. Origin 'null' is therefore not allowed access.

And the XMLHttpRequest error cannot load file: /// C: /% D0% A1% D0% 90% D0% 99% D0% A2% D0% AB /% D0% 93% D1% 80% D0% B0% D1% 84 % D0% B8% D0% BA% D0% B8 / highcharts / column-rotated-labels / data2.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

JQuery version 1.8.2. What did I do wrong to output the JSON array data? Prompt with arrays JSON never worked.

  • By default, receiving data from servers other than the current one is prohibited. To allow such an exchange, it is required to register the mentioned header (of course, the file system itself will not give it away); you can read about CORS if interested. Why not all local files are considered as one server - I can not say. - etki

2 answers 2

I think the problem is that you are testing not on a web server, just on drive C. Most likely, the browser cannot determine the host name (domain) and swears at your code.

  • Yes, that's right. I put it on the web server and the errors were gone, but still there is no output of the JSON array. How to display for example correctly in alert or console or in an html block array? - IntegralAL
  • one
    @IntegralAL, what is an html array? Simple objects are thrown into the console simply as an argument: console.log (obj, 'string', 12); // everything is ok - etki
  • because you don’t have json data in the data2.json file, here’s an example of json: {"success": true, "result": [{"category": "KAMERAS", "products": [{"product_id": "42" , "product_name": "Canon 1D MK I", "product_url": "Canon_1D_MK_I", "image": "a16ed3a7a71cd637f3d052b2cbf1ba11.jpg", "subcat_url": "Canon_Cameras", "subcat_name" "KAMAMER", "subcat_url": "Canon_Cameras", "subcat_name" "KAMAMER" ....... - Victor Halauko
  • one
    @Victor Halauko, there’s actually a single error in apostrophes instead of quotes (well, a comma in one place probably stands instead of a dot). - etki
  • 2
    @IntegralAL, well, respectively, also split into arrays $ data = array (array ('Shangai', 23.7), ...); @Victor Halauko what? [] is also JSON, just an empty array. I say the root element is not necessarily an object. - etki

@Victor Halauko

This is referred to as "{}".

They don't talk about anything. JSON spec:

A JSON text is a serialized object or array .

http://www.ietf.org/rfc/rfc4627.txt

Apparently it is implied that the remaining data types do not make sense to serialize; in any case, most parsers freely deserialize just a string or a number.

  • JSON - JavaScript Object Notation - Victor Halauko
  • one
    In javascript, almost any data type is an object. But I have already given speculation, I do not know what other evidence is needed. - etki
  • You are right: This is a JSON array containing two objects: [{"precision": "zip", "Latitude": 37.7668, "Longitude": -122.3959, "Address": "", "City": "SAN FRANCISCO" , "State": "CA", "Zip": "94107", "Country": "US"}, {"precision": "zip", "Latitude": 37.371991, "Longitude": -122.026020, "Address ":", "City": "SUNNYVALE", "State": "CA", "Zip": "94085", "Country": "US"}] - Victor Halauko