Solved

JQuery-AJAX- Basic help

Posted on 2011-09-10
4
408 Views
Last Modified: 2012-05-12
Trying to convert ajax calls over to Jquery.  Right now just in experiment mode for learning reasons.


Wanted I'm trying to do is transform the code below to accept returned JSON data and iterate through it (just alert functionality for now).  The click function belongs to an image.  The PHP page returns JSON data.

Or is there a better way in jquery for ajax calls that return JSON data?


                  $('#ok').click(function() {

                              $.ajax({
                                      type: "get",
                                      url: "agents_lookup.php",
                                      data: "term=a",
                                      success: function(msg){
                                          alert(msg );
                                      }
                                    });
                  
                  
                  });
0
Comment
Question by:carsRST
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 164 total points
ID: 36516471
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 168 total points
ID: 36516496
You may use this shortcut : http://api.jquery.com/jQuery.getJSON/

$('#ok').click(function() {
           $.getJSON(
                 "agents_lookup.php",
                 {"term":"a"},
                  function(msg){
                      alert(msg ); // if agents_lookup.php return a JSON object you will alert an [object Object] here, if it return a JSON string your will see it
                  }
           );
});
0
 
LVL 82

Accepted Solution

by:
hielo earned 168 total points
ID: 36516681
>>I'm trying to do is transform the code below to accept returned JSON data and iterate through it
Then just add the 'dataType':'json' to your options and internally jquery will convert the result into a javascript obect. Then you can iterate through it using "for - in":

$('#ok').click(function() {
     $.ajax({
          type: "get",
          url: "agents_lookup.php",
          data: "term=a",
          dataType:'json',
          success: function(msg){
               //alert(msg );
               for( var k in msg ){
                    alert( k + "=" + msg[k] );
               }
          }
     });
});

Open in new window

0
 
LVL 16

Author Closing Comment

by:carsRST
ID: 36516944
You guys are great.  

Thanks for the help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question