-2

I was wondering if it was possible to make a GET request with javascript, so it can update text without refreshing the page.

If this is possible, how can I make a get request with javascript & get the result/decode it from json?

I tried this from a past question:

function updateButton(){

    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", "http://xxxx.com/getSpecialSale.php", false);
    xmlHttp.send(null);
    document.getElementById("dicebutton").innerHTML=xmlHttp.responseText;
}

And, it completely stops the main thread, making the website unresponsive. What is wrong?

7
  • 1
    Don't pass false to xmlHttp Commented Jun 2, 2017 at 21:10
  • 2
    Async is set to false (third param of .open), so it will lock up the UI until it completes. Your code will need modification to work with async true. Commented Jun 2, 2017 at 21:10
  • Yes - But why would aysnc be needed to GET data? It is not performance I'm worried about, the script is not working even though it should on the main thread. Commented Jun 2, 2017 at 21:13
  • 1
    AJAX requests should be asynchronous. Fix that first. Is there any problem after that? Commented Jun 2, 2017 at 21:15
  • 1
    Please don't edit your question to remove the question; I've rolled it back to the original version as your edit invalidated all answers. If you no longer want this question associated with your account please use the "contact us" link in the footer of the page to contact Stack Overflow staff to have them sort that out. Commented Jun 16 at 2:54

4 Answers 4

1

Currently you set the async parameter to false, so the request is sent to the server and the browser waits for the response. To make an async request, just pass true as thrid param to open

xmlHttp.open("GET", "http://xxxx.com/getSpecialSale.php", true);

in addition to that, you have to register an callback, which waits for the response (and maybe to handle errors..)

xmlHttp.onload = function (e) {
    if (xmlHttp.readyState === 4) {
        if (xmlHttp.status === 200) {
            console.log(xmlHttp.responseText);
        } else {
           console.error(xmlHttp.statusText);
        }
    }
};
xmlHttp.onerror = function (e) {
    console.error(xmlHttp.statusText);
};

In addition to that a note from the mozilla docs

Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.

Sign up to request clarification or add additional context in comments.

1 Comment

he also needs to move the code that uses responseText to a callback.
1
var isAjax=false;
function updateButton(){
     if(!isAjax) { //Stops the user making a million requests per minute
        isAjax=true;
        var xmlHttp = null;

        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", "http://xxxx.com/getSpecialSale.php", true);
        xmlHttp.send(null);
        document.getElementById("dicebutton").innerHTML=xmlHttp.responseText;
        isAjax=false;
    }
}

OR jQuery...

$("#btnUpdate").click(function(){
    $.get("http://xxxx.com/getSpecialSale.php", function(data, status){
        $("#dicebutton").html(data);
    });
});

Comments

0

If you want to use async you will need some code modifications, ie the stuff that happens after the response completes needs to be in a callback function as follows:

function updateButton(){

    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", "http://xxxx.com/getSpecialSale.php", true);
    xmlHttp.onload = function () {
      document.getElementById("dicebutton").innerHTML=xmlHttp.responseText;
    };
    xmlHttp.send(null);

}

Comments

-1

Actually nowadays browser is modern enough to say that, it's best use fetch function instead of using XMLHTTPRequest() as it's easier to manage. and much simpler.


async function updateButton(){
    const response = await fetch("http://xxxx.com/getSpecialSale.php");
    document.getElementById("dicebutton").innerHTML=await response.text();
}

Comments

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.