douwendu2460 2010-10-28 11:22
浏览 81
已采纳

是我同一起源政策的代码受害者?

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">
<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"></script>
  <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>
  • 写回答

1条回答 默认 最新

  • drn61317 2010-10-28 11:45
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效