1

Please help me with my problem in displaying JSON data into my view..

my script is:

 $('#supplierId').change(function(){
 $.get("{{ url('api/dropdown')}}", 
    { option: $(this).val() }, 
    function(data) {
      var firstnameID = $('#firstnameID');
      $.each(data, function(index, element) {   
          firstnameID.val(element.first_name);
     });
  });
});

and my JSON reply is:

{"id":7,"first_name":"John","last_name":"Doe"}

the thing is when i tried to:

alert(element.first_name);

it says UNDEFINED, but when I:

alert(element);

it gives me the value of the last name which is Doe.. my question is how can I then access the other values like the ID and the first name..

EDITED:

this is my route:

Route::get('api/dropdown', function(){
$input = Input::get('option');
$supllier = Supplier::find($input);
returnResponse::json($supllier->select(array('id','first_name','last_name'))
    ->find($input));
});

Please help me with this one, This is my first time using JSON so im a bit confuse on how this works.

Best Regards -Melvn

5
  • 1
    the problem is in your js, what are your trying to do ? Commented Nov 22, 2013 at 12:27
  • 1
    rather than use alert(), try displaying it in the browser's console using console.log() ... the console handles these js objects much better than alert Commented Nov 22, 2013 at 12:39
  • Hello @VitKos i want to auto fill some text box with other user details like last_name, adress, etc automatically once i select a specific id on my select box.. Commented Nov 22, 2013 at 12:41
  • In terms of future usage.. you might want to consider using URL::route() or URL::action() to reference your AJAX script. I have created an AjaxController for this in the past.. Commented Nov 22, 2013 at 13:13
  • Browsers have consoles, use them to see the result of your request. If an error had occurred in your code the response may not be JSON at all, if you dont handle these exceptions with App::error. Commented Nov 22, 2013 at 13:36

2 Answers 2

3

Why are you using each? This should work:

$('#supplierId').change(function(){
 $.get("{{ url('api/dropdown')}}", 
    { option: $(this).val() }, 
    function(data) {
      var firstnameID = $('#firstnameID');

          firstnameID.val(data.first_name);

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

1 Comment

Thank you very much for that answer.. Im not really good on javascript and ajax.. i just did copied it on another site.. but this one really did the job! Im very glad right now Thank YOU VERY MUCH!
2

Ok, give this a try..

Explicitly state that what you're expecting back from the server is JSON using the dataType option in get().

$('#supplierId').change(function()
{
  $.get("{{ url('api/dropdown')}}", 
    { option: $(this).val() }, 
    function(data) 
    {
      var firstnameID = $('#firstnameID');
      $.each(data, function(index, element) 
      {   
        firstnameID.val(element.first_name);
      });
    },
    'json' // <<< this is the dataType
  );
});

Now you should be able to access the data using the dot syntax:

console.log("last_name: " + element.last_name);
console.log("first_name: " + element.first_name);
console.log("id: " + element.id);

I would add another few lines, just to check that you're getting back what you expect to see:

console.log("NEW ELEMENT"); // << indicator in the console for your reference
console.log(element); // << the whole "element"

5 Comments

Thanks for that code @msturdy, but im still getting the same error.. CONSOLE LOG IS: last_name: undefined 7:421 first_name: undefined 7:422 id: undefined 7:423 NEW ELEMENT 7:424 7 7:425 last_name: undefined 7:421 first_name: undefined 7:422 id: undefined 7:423 NEW ELEMENT 7:424 John 7:425 last_name: undefined 7:421 first_name: undefined 7:422 id: undefined 7:423 NEW ELEMENT 7:424 Doe
ok, put a console.log(data) in at the beginning of the function() to check what you're getting back from the server...
Thank you very much for trying to help me out with this one.. I really appreciated it and I learned also something new from you.. The error on my part was Im using each method..!
by the way.. Object {id: 7, first_name: "John", last_name: "Doe"} was the product of console.log(data);
aaah, then I was mistaken in my understanding.. I thought you would have more than one person in the JSON, and therefore looping through them! In which case the each() would be quite appropriate! :) glad you solved it!

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.