Solved

Ajax call to server using jquery

Posted on 2009-07-01
1
379 Views
Last Modified: 2012-05-07
Hello.  Am new to jquery and ajax.  I need to make an ajax call that will add or remove a user's favorite location on click.  The two values are passed in json: result and locationcode.  This is what I have so far, not sure how to construct the rest:


$('.add-remove-button').bind('click',function(){
		$.post(
			
		);
});

Open in new window

0
Comment
Question by:aznprncritic
1 Comment
 
LVL 16

Accepted Solution

by:
anoyes earned 500 total points
ID: 24759426
The docs for the $.post function are here: http://docs.jquery.com/Ajax/jQuery.post

But basically, your script will look something like this:

$('.add-remove-button').bind('click',function(){
  $.post('/path/to/script.php', data, function(resp) {
    alert(resp);
  });
});

data is just a variable here that contains the data you want to post to the script, and function(resp) is the callback function that is triggered when the request completes.  The body of the response is passed into the function as the first parameter, in this case I've used resp as the variable name.  Ideally you'd return the status of the save and alert the user if something went wrong.
0

Featured Post

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
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)

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