views:

113

answers:

3

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"&gt;
 <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"&gt;    
 </script>
 <script src="http://www.json.org/json2.js"&gt;&lt;/script&gt;
 <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>
A: 
var json = [{"options":"smart_exp"}, {"options":"user_int"}, {"options":"blahblah"}]

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

    alert(json[i].options)

}
Lorenzo
it doesn't work even with eval.
XCeptable
+3  A: 

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 .

AKX
XCeptable
Added clarification to my answer. HTH
AKX
XCeptable
The addition of http://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", ...).
AKX
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.
AKX
XCeptable
You haven't clarified -- where is the HTML page that has this code? It has to be on the same domain as the script it is retrieving data from, or it would violate the same origin policy.
AKX
XCeptable
Fine then; you should in that case be able to use just "conn_mysql.php" as the URL for $.getJSON. Have you tried serving a trivial bit of JSON, ie. <?php header("Content-type: application/json"); echo '{"status": "It works!"}'; ?> or such?
AKX
XCeptable
XCeptable
A: 

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.

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