0

In a jQuery ajax statement I want to receive a payload from the server and process it in another function. I am just having trouble with the syntax of the function.

    $.ajax({
    url: 'mypgm.pgm',
    type:'POST', 
    dataType: 'json',
    data:   'action=add' +
             '&techno=' + techno +
             '&activity=' + activity, 
    success: ajax_callback(msg),
    error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
        }       
    });
}

    function ajax_callback(msg) { 

        alert(msg);
    }
    

Response

{"response": {
    "success": "0",
    "message": "The Activity has been added."
    }
}
1
  • It looks like mypgm.php is responding, per the response you have here - what exactly is the problem? Commented May 18, 2011 at 23:36

3 Answers 3

3

A success callback handler for $.ajax() can take in the following values:

success(data, textStatus, jqXHR)

Source: http://api.jquery.com/jQuery.ajax/

So your function callback would be something like this:

function ajax_callback(data, textStatus, jqXHR)     
{
  alert(data.response)
}

Note how I created a named function this way, instead of assigning it to a var. If you only care about the data, you can just leave the last 2 parameters off:

function ajax_callback(data)     
{
  alert(data.response)
}
Sign up to request clarification or add additional context in comments.

6 Comments

Does this look like this? success: success(data, textStatus, jqXHR),
I'm not sure what you're asking. The format of a callback handler for success is just as you have pasted, and just as I have laid out in my code. Please give it a try and let me know if you have an issue.
@bob data is simply a parameter of the function ajax_callback. You could call it anything you wanted within the rules of javascript variable names. It could be cow, dog, lemon, whatever you want. What gives it a value is that the JQuery $.ajax code, upon successful completion of an AJAX code, passes the data, the text status, and the ajax object to the callback you defined.
@bob In turn, data gets assigned the data, textStatus gets assigned the text status, and jqXHR gets assigned the value of the ajax object.
Hi onteria - thanks for your patients. I revised my original example above. however I still get 'msg not defined error'
|
0

Try this:

$.ajax({
    url: 'mypgm.php',
    type: 'POST',
    dataType: 'json',
    data: 'action=add' + '&techno=' + techno + '&activity=' + activity,
    success: function(data) {
        alert(data)
    }
});

1 Comment

Thanks. I would like break the success function out.
0

You callback should be defined before the ajax call and change the signature of the function to:

var ajax_callback = function(data, textStatus, xhr) {
    // parse the data.responseText to json
}

You could alternatively just use $.getJSON()

1 Comment

not sure if we are sticking with json. .ajax seems more flexible.

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.