Solved

JQuery-AJAX- Basic help

Posted on 2011-09-10
4
394 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now