Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax callback dunce

I am having difficulty understanding how to implement a callback function. I've search around alot and found a lot of material on the subject but nothing that I've been able to implement in my jQuery code.

Can someone please edit my code here so I can copy it back into my test page to see exactly what I need to do?

What I need

First, declare a variable and give it a value. Second Execute my Ajax call and reference that variable. Sounds easy but I don't get it. Here's my code example if you would be so kind to help me.

var myName = 'Ann';

$.ajax(
{
    type:       "post",
    url:        "URL",
    cache:      false,
    dataType:   "json",
    data: 
    {
        xxxxxx: xxxxxxxxx
    },
    success: function(objResponse) 
    {
        //How can I do this????
        alert(myName);
    }
});
like image 238
Ann Avatar asked May 15 '26 22:05

Ann


1 Answers

seems like this should work? works for me every day.

var myName = 'lol';
$.ajax({
        type:       "post",
        url:        "URL",
        cache:      false,
        dataType:   "json",
        data: {
                xxxxxx: xxxxxxxxx
              },

        success: function(objResponse) 
        {
            //How can I do this????
            alert(myName);
        },
        error: function (xhr, err) { alert("Error: \n\nreadyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText, 'nosave'); }
    });

if the alert isn't firing, you should add an error to this, like i did above.

you should look into developing with firebug, you will be able to see all ajax calls you make.

like image 53
Chris Brickhouse Avatar answered May 18 '26 17:05

Chris Brickhouse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!