1
$(document).ready(function()
{

$.ajax({
method: "get",
url: 'ctr_seearmylist.php',
dataType: 'json',
data: 'get="squad"',
success: processSquads()
});

});

function processSquads(response)
{
alert(response);

}

Why isn't it working? How can I use the Json result that I'm getting? The Json result looks like this[[1,2]][[1,2]]

This is how the php function that is called looks like: {...... $temp[0]=$id; $temp[1]=$squad_id;

$result[]=$temp;
}
$result=json_encode($result);
return $result;

I looked at the response I get in firebug and I receive this: [["1","12"],["2","3"],["3","7"]] but I can't manage to write it out. I would want to write out just 1, 2, 3, but I can't even write it as it is.

I should mention that I tried to parse it using jQuery.parseJSON, but in the function process squad doesn't even get send the parameter. what am I doing wrong?

3
  • maybe functions should be declared BEFORE they are used? ;) Commented Jan 16, 2011 at 19:34
  • @shybovycha; it is declared before it's used, the call is when body has loaded (call back), the function is declared immediately. Commented Jan 16, 2011 at 19:36
  • @shybovycha: Declaring a function with the "function identifier(...){}"-syntax actually declares it in the beginning of its scope, so even if it wasn't used as a callback as roe pointed out, it would still work. Read about "hoisting" if you're curious. Commented Jan 16, 2011 at 20:18

3 Answers 3

2

Leave the parenthesis way from the callback definition:

$.ajax({
    method: "get",
    url: 'ctr_seearmylist.php',
    dataType: 'json',
    data: 'get="squad"',
    success: processSquads
});

If you want control over what is exactly passed into your callback method, you can wrap the definition into an anonymous function — e.g.:

$.ajax({
    method: "get",
    url: 'ctr_seearmylist.php',
    dataType: 'json',
    data: 'get="squad"',
    success: function(data, textStatus, XMLHttpRequest) {
         processSquads(data, XMLHttpRequest);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0
success: processSquads()

This line actually calls 'processSquads', I think you want jQuery to call it when it's done...

success: processSquads

HTH.

1 Comment

Thank you, now the function can see the parameter, but I still have a problem selecting only some things from it. I tried iterating through it in a for and addressing it by response.items[i] but it's not working
0

jQuery documentation states that:

success(data, textStatus, XMLHttpRequest)

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the 'dataType' parameter; a string describing the status; and the XMLHttpRequest object (available as of jQuery 1.4). This is an Ajax Event.

So, you have to make sure arguments are right.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.