Running JS code with JQuerry



  • I want to run the following code:

    <!DOCTYPE html>
    <html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
        <script src="qrc:///qtwebchannel/qwebchannel.js"></script>
    </head>
    <body>
    
    <script>
    var processImage;
    $(document).ready(function() {
    new QWebChannel(qt.webChannelTransport, function(channel) {
    mainWindow = channel.objects.mainProjectWindow;
    
    processImage = function getImageData(sourceImageUrl) {
    	function makeBlob (dataURL) {
    	    var BASE64_MARKER = ';base64,';
    	    if (dataURL.indexOf(BASE64_MARKER) == -1) {
    		var parts = dataURL.split(',');
    		var contentType = parts[0].split(':')[1];
    		var raw = decodeURIComponent(parts[1]);
    		return new Blob([raw], { type: contentType });
    	    }
    	    var parts = dataURL.split(BASE64_MARKER);
    	    var contentType = parts[0].split(':')[1];
    	    var raw = window.atob(parts[1]);
    	    var rawLength = raw.length;
    
    	    var uInt8Array = new Uint8Array(rawLength);
    
    	    for (var i = 0; i < rawLength; ++i) {
    		uInt8Array[i] = raw.charCodeAt(i);
    	    }
    
    	    return new Blob([uInt8Array], { type: contentType });
    	}
    
    
    	var subscriptionKey = "can't post this value here";
    	var uriBase = "https://southcentralus.api.cognitive.microsoft.com/face/v1.0/detect?";
    	var params = {
    	    "returnFaceId": "false",
    	    "returnFaceLandmarks": "false",
    	    "returnFaceAttributes": "age,gender"
    	};
    
    	$.ajax({
    	    url: uriBase + "?" + $.param(params),
    	    beforeSend: function(xhrObj){
    		xhrObj.setRequestHeader("Content-Type","application/json");
    		xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
    	    },
    	    type: "POST",
    	    data: '{"url": ' + '"' + sourceImageUrl + '"}',
    	})
    	.done(function(data) {mainWindow.getJSONArray(data);})
    	.fail(function(jqXHR, textStatus, errorThrown) {
    	    var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): ";
    	    errorString += (jqXHR.responseText === "") ? "" : (jQuery.parseJSON(jqXHR.responseText).message) ?
    		jQuery.parseJSON(jqXHR.responseText).message : jQuery.parseJSON(jqXHR.responseText).error.message;
    	    alert(errorString);
    	});
    };
    });
    
    });
    </script>
    
    

    The code that I originally used had a QWebEngineView, which doesn't work on Android; so I was wondering what approach to take to run this code. I saw the options to run JS on QT, but I'm not sure if I can run AJAX or JQuerry with them, so I was wondering if anyone has worked with this before trying the approaches in vain, or if I should take another approach entirely.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.