First of all i have the following to convert my Javascript array to a JSON array.

<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/blob/master/json2.js"></script>

  var sv_defaultArray = new Array([]);
  sv_defaultArray[0] = "post 4";
  sv_defaultArray[1] = "post 5";
  sv_defaultArray[2] = "post 6";

  var myJsonString = JSON.stringify(sv_defaultArray);

Now i would like to write the converted array to a json file lets say http://www.foobar.com/array.json via any conventional method possible.

So now i am at the following stage with php and jQuery.post facilities;

<?php
$list = $_POST["array"];
$fp = fopen('array.json', 'w');
fwrite($fp, $list);
fclose($fp);
?>

$.post("http://www.foobar.com/write.php", { 'array': myJsonString});

The above solution is giving me errors mainly returned in console as origin errors.

share|improve this question
which force do you believe should write that data on the target machine ? That needs to invoke some kind of server-logic obviously. – jAndy Aug 24 '11 at 8:57
i was writing an array via php before as a CSV string, so php potentially ? – Xavier Aug 24 '11 at 8:58
4  
yes, any server script will potentially do it. You only need to surf around the same-origin policy barrier. Since we're talking about JSON, json-padding seems appropriate. – jAndy Aug 24 '11 at 9:01
do you have any resources i could consult for writing the php code? Essentially i am just using php to read the json array as a text string and then write the text string as is to the .json file? – Xavier Aug 24 '11 at 9:05
1  
I think you cannot use Ajax to make a POST request to a different domain. You could, as already said, use JSONP. That has the disadvantage that the data you want to send is limited by the maximum length of a URL. You can create a form inside a hidden iframe and make the POST request through that form. – Felix Kling Aug 24 '11 at 9:33
show 4 more comments
feedback

3 Answers

up vote 1 down vote accepted

You can have a server side PHP file on the other domain accept your json as POST/GET paramter, and write it to a file, then return a json response as result.

Then you can use jquery.getJSON on your first domain to call the url with your json that needs to be written

http://api.jquery.com/jQuery.getJSON/

share|improve this answer
feedback

You need a server-side script that can write a file. Then you could send the json array to it through ajax and it will write the file.
I must stress the fact that javascript doesn't have access to the file-sistem, so it cannot be done with only pure javascript.

share|improve this answer
feedback

You will need to submit the data through a form as AJAX does not allow cross-domain requests. Everything else you are doing is 100% correct (assuming the php is on the destination server). I would recommend a text area with visibility:none style tag set. Use js to populate the textarea and submit the form using form.submit()

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.