views:

139

answers:

1

Here is my code. You have to kindly look does it suffer from 'same origin policy' in this shape. The domain for HTML is (http://127.0.0.1/jqload.html) & php file (http://127.0.0.1/conn_sql.php). This is json format : [{"options":"smart_exp"},{"options":"user_int"},{"options":"blahblah"}] I actually want to append json data that I receive in HTYML with user input & I am suffering in that. If I use eval for parsing, it works fine to point its put here. But if I use JSON.parse to parse, the whole code stops working & this error message is issued '"IMPORTANT: Remove this line from json2.js before deployment". I put my code for some other question on stackoverflow forum & I was told that my code suffer from 'same origin policy' that causes the problems in appending JSON data. So can you kindly see does my code suffer from this policy? Though I have doubts it suffers from that policy as I learn that it restricts if files reside on different domains, here both files reside next to each other.

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html><head></head>
 <body> 
  <form name="index">
   <p><input type = "text" id = "txt" name = "txt"></input>
   <p><input type = "button" id = "send" name = "send" value = "send" onClick= 
       "ADDLISTITEM"></input>
   <p><select name="user_spec" id="user_spec" />
  </form>
  <script>
  function ADDLISTITEM()
  {
      alert (json.length); // working
      alert json.options[1]; // do not show any value, message just for testing
      json.options[json.length+1] = 'newOption'; //not working; HELP HERE
      jsonString = JSON.stringify(json);//working fine for current data
      alert(jsonString);
      //here I have to implement send this stringify(json) back to server,HELP  
      //HERE     
  }                    
  </script>
  <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
  <script>
  var json;
  $(document).ready(function() { 
      jQuery .getJSON("http://127.0.0.1/conn_mysql.php", function (jsonData) {
          json = jsonData;
          $.each(jsonData, function (i, j) {
              document.index.user_spec.options[i] = new Option(j.options);
          });
      });
  });
  </script>
 </body>
</html>
A: 

You're on the right track, but here: $.each(jsonData, function (i, j) { ... } you are actually looping through each character of the jsonData string, and not the json object. Just change jsonData to json inside the $.each(), and it should work fine (regardless of whether you're using eval or JSON.parse, but I recommend the latter).

Edited: Since you are using jQuery.getJSON(), you don't need to use eval or JSON.parse - jQuery does it for you. So inside your callback function, just set json = jsonData .

<script>
  var json;
  $(document).ready(function() { 
      jQuery .getJSON("http://127.0.0.1/conn_mysql.php", function (jsonData) {
        json = jsonData;
        $.each(json, function (i, j) {
            document.index.user_spec.options[i] = new Option(j.options);
        });
      });
  });
</script>
Frode
XCeptable