Global Variable staying undefined with Ajax Response

Joined
Jan 6, 2022
Messages
2
Reaction score
0
I have made 30 attempts to try and update my global variable but it keeps saying undefined. I've tried setting a timer function, passing the response to a another function, etc. Can someone please tell me how to fix this?

JavaScript:
(function (d) {
var jsonobjvar; // < -- stays undefined

jQuery(document).ready(function ($) {
    var data = {
        'url': 'ajax.php',
        'action': 'my_other_action', // arbitrary
        'whatever_other': name      // arbitrary
    };
    // making a response alert with the data
    jQuery.post(data.url, data, function (response) {
        var jsonobj = $.parseJSON(response);
        alert('Got this from the server: ' + response);
            jsonobjvar = response;
            jsonobjvar;
        };
    });
});
})(document);
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
When are you checking this variable? It's not terribly global, because it's defined within that anonymous function. It goes out of scope when that function returns.
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
Can't help you with that hence i don't work with JQ.
Maybe you should check if response really returns an object or not.

What exactly is your alert splashs back? (By the way, bad idea to use alert for an object. Use console.log instead)

PLUS:

Try
Code:
var jsonobjvar; ///Now it's global
(function (d) {
instead of
Code:
(function (d) {
var jsonobjvar;

And what the h... should that be???
Code:
jsonobjvar = response;
jsonobjvar; <------??????????????????????????????????????????????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top