Why is XMLHttpRequest() seems to be unable to update?

Viewed 57

Here is my Html code:

<html>
<head>
<title>aerator</title>
</head>

<body>

<div id="train"></div>
<div id="predict"></div>

Here is my Javascript. I am trying to get the data using XMLHttpRequest for every certain set of interval for 2 variable to later using each of them to update the html with div id train and predict

$(document).ready(function(){
refresh();
});


function refresh()
{                                                                       
    setTimeoutfunction(){
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("predict").innerHTML = this.responseText;
                }
        };
        
        xhttp.open("POST", "http://127.0.0.1:5000/predict", true);
        xhttp.send();
    }, 1100);
    
    setTimeout{function(){
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("train").innerHTML = this.responseText;
                }
        };
        
        xhttp.open("POST", "http://127.0.0.1:5000/train", true);
        xhttp.send();
    }, 3000);
}

Below is my python flask code. I am including the needed function which i refer on my javascript code above. Edit : I actually have tried both POST and GET method and both is not retrieving data.

@app.route("/predict")
def mainPrediksi():
    predict = preprocessing('root','','127.0.0.1','database')

    if(condition):
        X = 'option1'
    else:
        X = 'option2'
    return (X,str(datetime.now()))

@app.route("/train")
def mainTrain():
    X_train,X_test,Y_train,Y_test = preprocessing('root','','127.0.0.1','database')

    X_train = X_train.astype(float)
    Y_train = Y_train.astype(float)

    #train classifier
    model = train_clf(X_train,Y_train)
    
    # save the model to disk
    filename = 'randomForest.sav'
    joblib.dump(model, filename)
 
    return ('Success Train Model',str(datetime.now()))

I am expecting that my XhttmlRequest to get the return value from /predict and /train, but from what I see in my terminal it seems to only post request and not getting any response. t Is there anything that I did wrong? thank you

2 Answers

Isn't the xhttp.open "POST" method for sending data? If you want to retrieve data, use the "GET" method?

For anyone who might want to know what is the problem, I actually manage to figure out that the reason why the post didn't give any response. It is because in flask you need to specify the methods to access the function otherwise your POST or GET request will be ignore.

@app.route("/predict",methods=["POST","GET"])--> added methods in here
def mainPrediksi():
    predict = preprocessing('root','','127.0.0.1','database')

    if(condition):
        X = 'option1'
    else:
        X = 'option2'
    return (X,str(datetime.now()))

Thanks for the community help and I hope my answer help whoever needed it.

Related