Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. XMLHttpRequest in JS using setRequestHeader
Servers for Qt installer are currently down

XMLHttpRequest in JS using setRequestHeader

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
7 Posts 4 Posters 2.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    filipdns
    wrote on 8 Jan 2019, 11:07 last edited by filipdns 1 Aug 2019, 11:08
    #1

    Hello,

    I want to get data from webradio and original script is:

    "use strict";
    
    const axios = require("axios");
    const qs = require("qs");
    //const { log } = require("abr-log")("meta-Djam Radio");
    
    module.exports = async function(exturl) {
    	try {
    		const req = await axios({
    			method: 'POST',
    			url: exturl,
    			data : qs.stringify({
    				origin: 'website'
    			}),
    			headers: {
    				'Accept': 'application/json, text/javascript, */*; q=0.01',
    				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    		}});
    		const parsedResult = req.data;
    		const curTrack = parsedResult["tracks"]["0"];
    		const picture = curTrack["pictures"][0];
    		return { artist:curTrack["artist"], title:curTrack["title"], cover: picture };
    	} catch (err) {
    		return { error: err };
    	}
    }
    

    I don't have axios in Qt then I try code below but it's returning "null":

    function djamradio(url) {
        var res
        var xhr= new XMLHttpRequest()
    xhr.open('POST', url)
    xhr.setRequestHeader('Accept','application/json, text/javascript, */*; q=0.01')
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
    xhr.onreadystatechange = function() {
    
            if (xhr.readyState === XMLHttpRequest.DONE) { // La constante DONE appartient à l'objet XMLHttpRequest, elle n'est pas globale
                var parsedResult = xhr.responseText;
                //var curTrack = parsedResult["tracks"]["0"];
                //title=curTrack["title"]
                console.log(parsedResult)
    
            }
    
        };
        xhr.send()
    }
    

    url is: https://www.djamradio.com/actions/infos.php

    How can I make it work please?

    thank you for your help

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Jan 2019, 13:18 last edited by
      #2

      Hi,

      I haven't used that class yet but following your original call, shouldn't you be sending some data ?

      Something like xhr.send(qs.stringify({ origin: 'website'})) ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 2 Replies Last reply 8 Jan 2019, 16:11
      0
      • S SGaist
        8 Jan 2019, 13:18

        Hi,

        I haven't used that class yet but following your original call, shouldn't you be sending some data ?

        Something like xhr.send(qs.stringify({ origin: 'website'})) ?

        F Offline
        F Offline
        filipdns
        wrote on 8 Jan 2019, 16:11 last edited by
        #3

        @SGaist Hi SGaist, happy new year!!

        May be but with xhr.send(qs.stringify({ origin: 'website'})) that mean I need
        var qs = require("qs");
        but I can not use require in .js in Qt becauce require is not defined message come on...

        1 Reply Last reply
        0
        • S SGaist
          8 Jan 2019, 13:18

          Hi,

          I haven't used that class yet but following your original call, shouldn't you be sending some data ?

          Something like xhr.send(qs.stringify({ origin: 'website'})) ?

          F Offline
          F Offline
          filipdns
          wrote on 8 Jan 2019, 16:18 last edited by
          #4

          @SGaist
          I try with JSON.stringify but result still empty

          function djamradio(url) {
              var res
              var xhr= new XMLHttpRequest()
              xhr.open('POST', url)
          
              xhr.setRequestHeader('Accept','application/json, text/javascript, */*; q=0.01')
              xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
              xhr.onreadystatechange = function() {
          
                  if (xhr.readyState === XMLHttpRequest.DONE) { // La constante DONE appartient à l'objet XMLHttpRequest, elle n'est pas globale
                      var parsedResult = xhr.responseText;
                      //var curTrack = parsedResult["tracks"]["0"];
                      //title=curTrack["title"]
                      console.log(parsedResult)
          
                  }
          
              };
              xhr.send(JSON.stringify({ origin: 'website'}))
          }
          
          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 8 Jan 2019, 21:05 last edited by
            #5

            Then I would check the difference in the request sent using a tool like Fiddler.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JBonilla
              wrote on 21 Feb 2019, 02:19 last edited by
              #6

              You have to check that the request was succesfull xhr.status == 200, probably the server answers you with an error and this is why you are getting xhr.responseText = null.

              https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp

              Inspect if you are getting an http error code in xhr.status and the number to get more info.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                riyas
                Banned
                wrote on 5 Mar 2019, 11:31 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved