Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery-AJAX- Basic help

Posted on 2011-09-10
4
Medium Priority
?
418 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 656 total points
ID: 36516471
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 672 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 672 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

722 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