Solved

how to make ajax call on click event

Posted on 2014-04-14
2
193 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
ID: 39998641
$( "#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
ID: 40018420
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

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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