Solved

how to make ajax call on click event

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
exchange ,script 10 54
Permission on folders 6 20
CSRF session and form tokens never match when using php/AJAX 4 25
add if statement powershell 8 16
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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