0

I'm trying to load a site via GET in JavaScript embeeded in QWebView. If I use jQuery.ajax it gives me an error. The solution is to take the XMLHttpRequest.

This is my JavaScript code:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var data = jQuery.parseXML(xmlhttp.responseText);
        alert(data);
    } else if (xmlhttp.readyState == 4) {
        alert(JSON.stringify(xmlhttp));
    }
}

xmlhttp.open("GET", CO_SERVICE, true);
xmlhttp.setRequestHeader("Authorization", "Basic " + btoa("user:password"));
xmlhttp.send();

It works just fine in Chrome. If I execute my Qt application and load the file with:

QNetworkProxyFactory::setUseSystemConfiguration(true);

ui->webView->load(QUrl("qrc:///index.html"));
ui->webView->show();

I get this error:

Error message

What am I doing wrong?

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
Niklas
  • 23,674
  • 33
  • 131
  • 170
  • This is probably what the `alert(JSON.stringify(xmlhttp));` call produces. Cause you stringify the xmlhttp object. – Jite Apr 02 '14 at 11:49
  • @Jite I am aware of that, but still there is some error. The response is null and the fact that it jumped into the else if is a proof that an error occurred. – Niklas Apr 02 '14 at 11:58
  • Well, if it jumps in the `else`, its probably because the script returns another status than 200. I can't see the status property in the json data, so not sure which, but maybe you are requesting a page that don't exist? – Jite Apr 02 '14 at 15:31
  • @Jite `"status": 0`. Could this be, cause my network uses a proxy? However I am able to load a dependency in JS, which is located on an internal server. – Niklas Apr 03 '14 at 07:29
  • Check this answer for info on status code 0: http://stackoverflow.com/a/12622082/1980359 . Its not impossible that the proxy could be a issue, could you try the script without the proxy and see if that works? – Jite Apr 03 '14 at 07:55
  • @Jite I tried that and it also does not work. – Niklas Apr 03 '14 at 11:40
  • If you visit the url in the browser, does it give the correct response? – Jite Apr 03 '14 at 11:49
  • @Jite Yes, but I have to add a username and password[(like described here)](http://qt-project.org/forums/viewthread/16827), since my backend requires basic authentication. – Niklas Apr 03 '14 at 12:11
  • @Jite I found a solution. Look at my answer below. – Niklas Apr 03 '14 at 13:16
  • Glad you got it working! Good luck. :) – Jite Apr 03 '14 at 13:33

1 Answers1

0

This is my solution, which worked for me:

JavaScript

this.done = function (value) {
    console.log(value);
};

getRequest.done.connect(this, done);
getRequest.get("url", "username", "password");

C++

MainWindow.h

private:
    Ui::MainWindow *ui;
    GetRequest* mGetRequest;

private slots:
    void addJSObject();

MainWindow.cpp

mGetRequest = new GetRequest();

QObject::connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJSObject()));

ui->webView->load(QUrl("qrc:/index.html"));


void MainWindow::addJSObject() {
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("getRequest"), mGetRequest);
}

GetRequest.h

#ifndef GETREQUEST_H
#define GETREQUEST_H

#include <QObject>
#include <QString>

#include <QNetworkReply>
#include <QNetworkAccessManager>

class GetRequest : public QObject {
    Q_OBJECT

public:
    explicit GetRequest(QObject *parent = 0);
    ~GetRequest();

public slots:
    void get(QString url, QString username, QString password);

signals:
    void done(QString value);

private:
    QNetworkAccessManager* mNetworkAccessManager;

private slots:
    void replyFinished(QNetworkReply* networkReply);
};

#endif // GETREQUEST_H

GetRequest.m

#include "getrequest.h"

GetRequest::GetRequest(QObject *parent) : QObject(parent) {
    mNetworkAccessManager = new QNetworkAccessManager(this);
    QObject::connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}

GetRequest::~GetRequest() {
    delete mNetworkAccessManager;
}

void GetRequest::get(QString url, QString username, QString password) {
    QUrl u(url);
    u.setUserName(username);
    u.setPassword(password);

    mNetworkAccessManager->get(QNetworkRequest(u));
}

void GetRequest::replyFinished(QNetworkReply* networkReply) {
    QString value(networkReply->readAll());

    emit done(value);

    delete networkReply;
}
Niklas
  • 23,674
  • 33
  • 131
  • 170