1

i'am trying to send getVenueFromFS returned value to RealTimeUpdate.php page without success. the value is printed correctly using the alert method but i cant send it, once the project is complete it should be put on uni servers, they dont have ajax or an other 3rd party's. the function: `

    <script type="text/javascript">
             <!--
                function getVenueFromFS() {
                    var venueFSID = "4a1ef31ff964a520ef7b1fe3";
                    var xhttp = new XMLHttpRequest();
                    var url = 'https://api.foursquare.com/v2/venues/'+venueFSID+'?oauth_token=MOBUA5IQL5PJGHPMAEU22OQDWIJJWF425XO5JLOSFHGJOTAC&v=20161224';
                    xhttp.onreadystatechange = function() 
                    {
                        if (this.readyState == 4 && this.status == 200) 
                        {
                           var obj = JSON.parse(xhttp.responseText);
                            alert("venue number: " + venueFSID + " was retrieved successfully"); 
                        alert("like count: " + obj['response']['venue']['likes']
                           return obj['response']['venue']['likes']['count'];
                        }
                    };
                    xhttp.open("GET", url, true);
                    xhttp.send();
                }

             //-->
        </script>` 
    but  using              //$.post('RealTimeUpdate.php', {variable: javascriptVariable});
    or 
                    //window.location.href = "RealTimeUpdate.php?name=" + javascriptVariable;  wont work.

}

Soultion: i put the script inside the body, and not the head, disable the returned value and wrote another function that will retrieved it, and used setTimeout , thanks everybody

function myJavascriptFunction() { 
            var data = getVenueFromFS();
            //alert("NEW" + data);
            //$.post('RealTimeUpdate.php', {variable: data});
            //window.location.href = "RealTimeUpdate.php?name=" + data; 
            var FSID = document.getElementById('FsId').value;
            window.location.href = "RealTimeUpdate.php?name=" + data + "&FSID=" + FSID; 
        }
        function getVenueFromFS() {
            var obj
            //var venueFSID = document.getElementById('FsId').value;
            var venueFSID = "4a1ef31ff964a520ef7b1fe3";
            var xhttp = new XMLHttpRequest();
            var url = 'https://api.foursquare.com/v2/venues/'+venueFSID+'?oauth_token=MOBUA5IQL5PJGHPMAEU22OQDWIJJWF425XO5JLOSFHGJOTAC&v=20161224';
            xhttp.onreadystatechange = function() 
            {
                if (this.readyState == 4 && this.status == 200) 
                {
                    obj = JSON.parse(xhttp.responseText);
                    alert("venue number: " + venueFSID + " was retrieved successfully"); 
                    alert("new like count: " + obj['response']['venue']['likes']['count']);
                    parameter = obj['response']['venue']['likes']['count'];
                    setTimeout(function(){ window.location.href = "RealTimeUpdate.php?name=" + parameter }, 6000); 
                   // return obj['response']['venue']['likes']['count'];
                }
            };
            xhttp.open("GET", url, false);
            xhttp.send();`enter code here`
            return obj['response']['venue']['likes']['count'];
        }

1 Answers1

-1

I'm not sure I correctly understood, what You would like to achieve, but I suppose

1. xhttp should be sync, so xhttp.open("GET", url, false); // ASYNC -> false
2. var data = getVenueFromFS(); 
   $.post('RealTimeUpdate.php', {variable: data });

If XMLHttpRequest is async, then flow is not as expected ;)

HubertS
  • 828
  • 6
  • 10
  • iam trying to update my database with the latest value stored under obj['response']['venue']['likes']['count'] , so the first step would be to get the new value, send it to php, and then preform regular update query with the new value. the problem is that the method $.post('RealTimeUpdate.php', {variable: data }); send undefined value, in contrast to the printed value with is 283 – Liran Siman Tov Jan 17 '17 at 17:26
  • Are You sure, url is correct ? In PHP file You could write `echo "Hello Liran";` and then You could observe in browser console (network section), whether response is visible .[chrome console](https://developers.google.com/web/tools/chrome-devtools/console/) As I wrote earlier, problem could be with async connection. This JQuery function let You set async to false: `$.ajax({ type: 'POST', url: 'RealTimeUpdate.php', data: {variable: data }, success: success, async:false });` – HubertS Jan 17 '17 at 19:44
  • Recently some browsers have started throwing warnings on synchronous requests on the main thread, and in the future this is likely to be blocked. No, setting async to false is not a proper solution. – CBroe Jan 18 '17 at 08:40
  • @CBroe Yes, I agree with You, that better to use are callbacks or even better promises, but I think, at the beginning can check synchronous request ;) – HubertS Jan 18 '17 at 09:55