views:

80

answers:

2

I have a php script which produces the JSON object.

In my jquery I am using this code to call the php script.

  $.getJSON("/st/std.php",
   function(data){
    alert(data);
       $("myspan").text(data);
   }
  );

How Do i get the Data from THE JSON Encoded in php File .In the form Iam entering some code which will perform the ajax lookup to a php file This is the Json Object iam producing it in php

Suppose if we input the "AAA" in the textbox it should fetch "AAA-ANAA".It should search the JSOn encoded object and fetch the label.

[{"label":"AAA-ANAA (PF)","value":"AAA"},
{"label":"AAB-ARRABURY (AU)","value":"AAB"},
{"label":"AAC-AL ARISH (EG)","value":"AAC"},
{"label":"AAE-ANNABA (DZ)","value":"AAE"}}
A: 

This is the expected behaviour. $.getJSON automatically calls $.parseJSON(response) before passing it to your callback. You can explore the JSON using Firebug, or if you already know the format of your JSON, you can navigate the response as you would an ordinary object, e.g.,

$('span').text(response.child[2])
Steven Xu
A: 

To see what ("object object") actually holds, replace alert(data); with alert(data.toSource());.

Say your php page returns {"a":"value"}, then $("myspan").text(data.a); would stick "value" in your span.

To pass a parameter, put it in the query string: "/st/std.php?param=value".

DaveS
$.getJSON("std.php?term="+$("#txt1t").val(), function(data){ alert(data); alert(data.toSource); $("#myspan1").text(data.toSource()); } );
Someone
Is this the correct way of passing the parameter to php file from jquery
Someone
Looks right. But you changed /st/std.php to just std.php, and you left the () off the end of your first data.toSource.
DaveS
@dave:Still getting Undefined value
Someone
Is your json well formed? jQuery's parseJSON function (which getJSON uses) isn't as tolerant as javascript's object notation. So, while {a:'a'} is a valid JS object, it isn't a valid JSON object. Run it through http://jsonlint.com/
DaveS
$("#txt1").autocomplete({ source: "/st/std.php", minLength: 1, select: function(event, ui) { var label= ui.item.label; alert(label); var value= ui.item.value; alert(value); $('#myspan').text(label.substr(value.length+1)); }, change: function(event, ui) { if (jQuery.trim($('#txt1').val()).length<3) { $('#myspan').text(""); $("#myspan").text("3 characters"); $('#myspan').val(''); } }})Earlier I used this code which I used to get the value and but autocompletion is spoiling my form so iam using $.getJSOn()
Someone
What is the JSON that std.php is giving you back?
DaveS
[{"label":"AAA-ANAA (PF)","value":"AAA"},{"label":"AAB-ARRABURY (AU)","value":"AAB"},{"label":"AAC-AL ARISH (EG)","value":"AAC"},{"label":"AAE-ANNABA (DZ)","value":"AAE"},
Someone
So, an array...Then try data[0].label, data[0].value, data[1].label, etc.
DaveS
No It is not coming when i say that
Someone
Can you update your original question with more code, like the JSON response and code you've tried parsing it with?
DaveS