Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Javascript



  • I am new to javascript and qml, I am currently working on a task I found online. Here is the javascript function.

    function request() {
            var base = "https://api.themoviedb.org/4/list/1?api_key=c2bfbeddbb087523d43eb817f5c7d40d"
            var access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJjMmJmYmVkZGJiMDg3NTI
            zZDQzZWI4MTdmNWM3ZDQwZCIsInN1YiI6IjVkMmY1MDdlMjdkOWNjNDE2ZDdiZDQ2OCIsInNjb3BlcyI6WyJhcGlf
            cmVhZCJdLCJ2ZXJzaW9uIjoxfQ.VL4AzMrddLht-8LAOGOtwlyajkJSfXX2hJj6Es7nfWg"
            var xhr = new XMLHttpRequest();
            xhr.open("GET", base);
            xhr.setRequestHeader("authorization","Bearer <<access_token>>")
            xhr.onreadystatechange = function() {
                if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
                    print('HEADERS_RECEIVED')
                } else if(xhr.readyState === XMLHttpRequest.DONE) {
                    print('DONE')
                    var json = JSON.parse(xhr.responseText.toString())
                    for(var i = 0; i<json.results.length;i++){
                        movieModel.append({"imageUrl": base + json.results[i].poster_path,"name":json.
                        results[i].original_title,"date": json.results[i].release_date,"stars":json.results[i].vote_average})
                    }
                }
            }
            xhr.send();
        }
    ```.
    Everything works fine , except for the imageUrl where I am getting the error 
    

    QML Image: Host requires authentication

    From what i could find, the error might be due to the fact that im using the base url in constructing the url of the image. I should use the url of that point in the website. Can someone help me out please?


  • I don't see anythin in your code that makes this a Qt or QML issue, but to be fair, I don't know QML. It seems to me to be a pure javascript debugging issue.


  • Lifetime Qt Champion

    @Abdulmueez said in Javascript:

    I am getting the error

    What error?

    "xhr.setRequestHeader("authorization","Bearer <<access_token>>")" - I'm really not an JavaScript expert, but shouldn't you put the content of access_token into the second string? Because now second parameter is just "Bearer <<access_token>>" string, it does not contain the actual token.



  • @Kent-Dorfman Yes, it was a javascript issue, already fixed it, the url for the image is different. Thanks a lot


  • Lifetime Qt Champion

    @Abdulmueez So please mark this topic as SOLVED then. Thanks!



  • How do I go about that please


  • Lifetime Qt Champion

    Hi,

    Click on the "Topic Tools" button and you'll have the option.


Log in to reply