1

I am trying to get the jquery getJSON function to work. The following seems very simple yet it doesn't work.

$("#edit-item-btn").live('click', function() {
    var name = this.id;
    $.getJSON("InfoRetrieve",
       { theName : name },
       function(data) {
       alert(name);
    });
});
3
  • agreed, format that. Is it alerting anything at all? or not even getting to that? If it's not alerting I can only assume your ajax path is incorrect or not returning something properly Commented Jan 6, 2010 at 4:10
  • this code sample has too few }s. please edit for entire code - unless thats the problem of course :-) what specifically 'doesnt work' Commented Jan 6, 2010 at 4:14
  • Thanks Simon - well for now I just want the alert to work, but that is not happening. I am now getting the request sent (it looks like a get request) so that is a good start. Commented Jan 6, 2010 at 4:23

5 Answers 5

3
  1. Make sure the surrounding code is working with your DOM by replacing the getJSON call with a simple alert
  2. Make sure the "InfoRetrieve" path actually exists. If you replace your file name in the URL bar with InfoRetrieve does it return JSON?
  3. function(data) needs to be closed with a } before you close the click handler.
  4. { theName : theName } makes more sense to me as the data. Are you sure you entered that right?
Sign up to request clarification or add additional context in comments.

1 Comment

Does the servlet have to return a valid JSON message to get the alert to work. I was hoping to sort that out later, once I got the piece of the request in place. Judging from the reading I have been doing it does, so this is what I am working on now.
2

You are using name variable on two places, but this variable is never defined.

Comments

2

Shouldn't you be doing alert(data)?

Comments

1

Have you tried using Firebug or Chrome Developer Tools to see what requests are being made?

Does a file called InfoRetrieve exist in the current path of your site? What does it return?

2 Comments

I've tried but I can't see any requests being made when I use Firebug, this is one of the main sources of confusion. However the button click is being registered, I previously used an alert to figure that out.
Yes InfoRetrieve is a servlet, I won't be using InfoRetrieve once this is working, but for now I just want to get the alert working.
0

I'm not convinced your code is correct. Try this and see what happens:

$("#edit-item-btn").live('click', function() {
    var name = this.id;
    $.getJSON("InfoRetrieve",
       { theName : name },
       function(data) {
           alert(data.name);
    });
});

This should work if InfoRetrieve responds with a JSON string like this:

{"name":"Sally Smith"}

A few things to note:

  1. You are sending a request to ./InfoRetrieve in the same directory as the page is located. If you are using a servlet, is that actually correct? Or do you want "/servlet/InfoRequest"?
  2. You are sending data as input to InfoRetrieve with a key of "theName" and a value of whatever is "this.id" is. Does your servlet know how to accept this input?
  3. You are then receiving back a response from InfoRetrieve, and "data" is set to an object that represents the json in the response. You need to then access properties of data to get at values in the response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.