admin管理员组

文章数量:1287595

I want to make a page on domain "example1" and get/parse a JSON file on another domain "example2/json.json". Can the json file be generated with javascript (on example2)? I think this can be done with php, but I want to do it with javascript. If it is not possible to generate a json file with javascript, is it possible to get/parse an object from a javascript file? EX: "example1" to "example2/js.js"

EDIT: Ok, so it is not possible to get/parse an object from a javascript file, because it is client side. So my only option is to generate a JSON file. Is it possible to do that with Javascript? I know it's probably not the best way, but I want to do it in JS, not PHP.

I want to make a page on domain "example1." and get/parse a JSON file on another domain "example2./json.json". Can the json file be generated with javascript (on example2.)? I think this can be done with php, but I want to do it with javascript. If it is not possible to generate a json file with javascript, is it possible to get/parse an object from a javascript file? EX: "example1." to "example2./js.js"

EDIT: Ok, so it is not possible to get/parse an object from a javascript file, because it is client side. So my only option is to generate a JSON file. Is it possible to do that with Javascript? I know it's probably not the best way, but I want to do it in JS, not PHP.

Share Improve this question edited Apr 10, 2013 at 12:31 asked Apr 10, 2013 at 1:24 user1834464user1834464 6
  • So you want the server (example2.) to serve a JSON file, but you want it to serve that file "using" Javascript? Meaning you want to run Javascript on the server? If that's the case, nodejs might be what you're looking for - nodejs – Snixtor Commented Apr 10, 2013 at 1:39
  • I want to generate a JSON file, but I want to do it with javascript. So I would need to use nodejs? – user1834464 Commented Apr 10, 2013 at 2:08
  • If you want to run javascript anywhere other than in a web browser, then you need a server-side javascript implementation. Nodejs is a recent notable example, but there are many others - en.wikipedia/wiki/… – Snixtor Commented Apr 10, 2013 at 2:51
  • Why do you specifically want to generate the JSON using javascript? There's a lot of different ways to produce JSON and javascript isn't necessarily the best choice. Have you already got javascript source code producing the JSON? – Snixtor Commented Apr 10, 2013 at 3:50
  • I have the javascript code generating objects, but not a JSON file. I need to put the object I generated into a JSON file so I can get and parse those objects from another website. – user1834464 Commented Apr 10, 2013 at 12:19
 |  Show 1 more ment

4 Answers 4

Reset to default 2

json is just a javascript object in a string format so javascript can make json data:

   var animal = {name: "cat", sound: "meow"};
   json = JSON.stringify(animal); //json is a JSON string

javascript does not allow ajax calls across to other sites (cross-site scripting) though becuase it is a security risk. you could look into jsonP which is a work-around this rule.

Also you cannot call to another website or your own server to run javascript to return something becuase javascript only runs in the clients browser (unless you are using a javascript server like node.js). A server has to be listening somewhere for a request.

To parse a JSON object with Javascript, I would use jQuery.

http://api.jquery./jQuery.parseJSON/

Like anthonybell said most browsers do not allow cross-site scripting so you need to look into jsonP or work on the same domain.

Also to generate JSON you can create an object using javascript then either loop though it and output it or just output your data in JSON format which you can read about here:
http://www.w3schools./json/

You can use JSONP - essentially wrap your JSON in a callback function on domain2. like so:

jsonCallback(
    {
        "sites":
        [
            {
                "siteName": "JQUERY4U",
                "domainName": "http://www.jquery4u.",
                "description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips & Code Snippets."
            },
            {
                "siteName": "BLOGOOLA",
                "domainName": "http://www.blogoola.",
                "description": "Expose your blog to millions and increase your audience."
            },
            {
                "siteName": "PHPSCRIPTS4U",
                "domainName": "http://www.phpscripts4u.",
                "description": "The Blog of Enthusiastic PHP Scripters"
            }
        ]
    }
);

Then you can call it from domain1. like so:

(function($) {
var url = 'http://www.jquery4u./scripts/jquery4u-sites.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);

You cannot replace PHP with JavaScript (unless you've got NodeJS or something).

Generating your JSON using PHP will return JSON to the client.

Having Javascript generate your JSON will return Javascript to the client. If you're doing that theres no point even usng JSON.

If you want to have a JSON Document generated (from data sources on your server) instead of a static JSON file you will need some sort of server-side programming.

If you want to use JavaScript you will need to install and configure NodeJS (and possibly some other services like a web-server, or create a server with NodeJS) and write scripts that generate and serve your JSON.

Creating the scripts to generate the JSON (and creating the basic server to host it) is not all that difficult. Installing and configuring Node can be more difficult.

If you already have access to PHP (and webserver) on your server, use that.

If your output file can be static, (and your running windows) you could possibly use JScript to generate your static file. This would require you to physically run the script (or possibly create a scheduled task to run it for you). So you'd be replacing your static file with newly generated JSON every so often. Probably not what you want though!

本文标签: jqueryCan I generate a JSON file with javascriptStack Overflow