0

This is format of JSON data: [{"options":"smart_exp"},{"options":"user_int"},{"options":"blahblah"}] that I receive through getjson from server. I need to append json with user input. I am trying to do it in this way: 1st convert it into javascript object, append it with user input, again convert to json object & send it back to server for database update. I have converted json to javaScript object using eval(). Now not able to manipulate javascript object. If I convert javascript object back to json object, it displays correctly all data that was sent from server.

 <!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>
 <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()
 {// this script suffers from errors on eval/JSON.parse methods
 alert (json.length);//outputs corrcet with eval 
 tring = JSON.stringify(json);//outputs corrcet with eval
 alert(jsonString);//outputs corrcet with eval
 alert(json.options[0]);//no output
 }
 </script>
 <script src="http://code.jquery.com/jquery-latest.min.js">    
 </script>
 <script src="http://www.json.org/json2.js"></script>
 <script>
 var json;
 $(document).ready(function() { 
 jQuery .getJSON("http://127.0.0.1/conn_mysql.php", function (jsonData) {
 json = eval(jsonData);
 //json = JSON.parse(jsonData);/*error if uncomment:"IMPORTANT: Remove this line from  
                               json2.js before deployment"*/
 $.each(jsonData, function (i, j) {
 document.index.user_spec.options[i] = new Option(j.options);
 });});
 });
 </script>
 </body>
 </html>
6
  • 2
    Your HTML code is kind-of broke... Commented Oct 27, 2010 at 11:11
  • 1
    Don't use eval() to parse your JSON. Use JSON.parse(), and include this file for older browsers compatibility : json.org/json2.js Commented Oct 27, 2010 at 11:13
  • In addition to Golmote's advice: 1. Use a doctype, 2. You don't need the language and type attributes on the SCRIPT elements, 3. Consider using the convention that uppercase function names are used only for constructor functions 4. Consider declaring global variables before declaring global functions that use those variables 5. Use lowercase tag names 6. Consider placing SCRIPT elements at the bottom of the page (right before </body>) Commented Oct 27, 2010 at 11:22
  • If I use JSON.parse(jsonData), the script stops working. i.e. the dropdwon list is not populated & json.length is also not showing any result. It shows this alert, "IMPORTANT: Remove this line from json2.js before deployment" Commented Oct 27, 2010 at 12:54
  • If I remove the include json.org/json2.js, the warning message is gone. It works i.e. executed json.length with eval() BUT JSON.pase() stops it working i.e. even populating the list. I edited the code to see how I use JSON.parse Commented Oct 27, 2010 at 13:15

3 Answers 3

3

In jQuery, $.getJSON()'s callback gets called with parsed JSON data; just use it.

$.getJSON("*.php", function(data) {
   $.each(data, function() { alert(this.options); });
);

should give you an alert for every {"options": "xyzzy"} object in the array.

EDIT after OP edited their post: Your edit clarifies things a little: You won't get any data back -- and it will be completely silent, too, as I found out -- if you violate the same origin policy.

Basically (with exceptions (preflight checks, etc)), you can only access URLs on the exact same domain via AJAX. If your HTML file is a static file served locally, it can not access http://127.0.0.1/; if your file is http://foo.baz.quux.org/, you can't simply AJAX into http://mordor.baz.quux.org .

Sign up to request clarification or add additional context in comments.

11 Comments

actually I need to use this script to append JSON data(that I receive from server & build a dropdown list) with user input & send it back to server to save & send a new set updated JSON to render. By echoing/alert I am trying to see I receive data & manipulate it. I edited the code.
Added clarification to my answer. HTH
Can u kindly tell whats solution in presence of this conflict. But as I stated above, the list get populated by server data & eval also parses json to javascript, as json.length executes but I am not able to manipulate it.
The addition of 127.0.0.1/conn_mysql.php to your example hints at you trying to do cross-domain AJAX, which is not directly possible. If the HTML file does indeed reside next to the conn_mysql.php script, then just use $.getJSON("conn_mysql.php", ...).
getJSON will give your callback JSON already parsed (provided you serve it as such -- maybe in all cases, not sure). But if you are attempting to violate the same origin policy, you will get nothing -- a blank string, maybe, or null, in your callback function.
|
0

I don't think the problem here has anything to do with eval/parse etc or the same origin policy. Your json is an array of objects each containing a member named options. Therefore you cannot do json.options[0], you have to do json[0].options.

2 Comments

Should have added this as a comment to AKX's answer, but it looks like I can't comment on other peoples' stuff yet.
thanX, you are right. I have done it already in the same way as you write here i.e. json[0].options
0
var json = [{"options":"smart_exp"}, {"options":"user_int"}, {"options":"blahblah"}]

for (var i = 0; i < json.length; i++){

    alert(json[i].options)

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.