Solved

JQuery-AJAX- Basic help

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 49
How to init a part of Javascript based on PHP value for Datatables & Yadcf 8 48
asp.net mvc5 6 17
html Uncheck Checkbox 2 8
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
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…

809 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