Solved

how to make ajax call on click event

Posted on 2014-04-14
2
189 Views
Last Modified: 2014-04-28
is this the correct approach

 $( "#rights").click( function() {
	  alert("sanu");
	  $.ajax({
          type: "GET",
          dataType: "json",
          url: "/tile/mainRights.html",
      
          success: function(json,sttext,response) {
        	  
          }
          if(json != null && json.length > 0) {
        	  
        	  for(i = 0; i < json.length; i++) {
        	       var obj = json[i].code;
        	       var obj1 = json[i].descrip;

          
         }
});

Open in new window

0
Comment
Question by:roy_sanu
2 Comments
 
LVL 5

Expert Comment

by:jayakrishnabh
Comment Utility
$( "#rights").click( function() {
 $.ajax({
                    type: 'GET', //or POST
                    url: 'WebForm1.aspx/MyMethod',
                    data: { },
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    success: function(data){
                            //do ur stuff here from data
                     }
                });
});
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
Comment Utility
greetings roy_sanu, , you ask "is this the correct approach", and if you use the code you posted, I would say NO. The AJAX request in the setup has this -
      url: "/tile/mainRights.html"

but you set the dataType to "json"  I can not see a GET Ajax request to a .html page as having much possibility of sending back a CSV JSON string.

If you set the dataType to "json" , then There is ONLY one type of Ajax response allowed from the server (mainRights.html  in this case), and that's a properly formatted javascript Object Notation string using { } and [ ].

Also in the success: function( ) you expect an Object that has Many elements with "code" and "descrip" properties as code -
var obj = json[j].code;
var obj1 = json[j].descrip;

this may be awkward to handle depending on what you are trying to display on your updated page.

If you can describe what you need to do for your page update, and what you know that the server page  mainRights.html  will send back to AJAX, then we can see what is "the correct approach".
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

8 Experts available now in Live!

Get 1:1 Help Now