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
Solved

how to make ajax call on click event

Posted on 2014-04-14
2
194 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 34

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
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…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

791 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