Cross origin error with QNetwork webassembly

Viewed 476

I am trying to make a post/get call using QNetworkAccessManager in my c++ code which is exposed to webassembly using emscripten.

When the exposed function is compiled and loaded in Javascript, the requests are getting blocked due to cross-origin request.

I am running the local server from the folder that contains wasm, js and html files.

[Edited]: After some reading on CORS, I tried by adding additional header parameters to request: request.setRawHeader("Access-Control-Request-Origin", "*");

But this also didn't work.

QNetworkRequest request(this->server);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader("Access-Control-Allow-Origin", "*");    request.setRawHeader("Access-Control-Allow-Methods", "POST");

// building the json and network request object
QByteArray byte_payload = QJsonDocument(payload).toJson();
QEventLoop loop;
QObject::connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
loop.exec();

std::cout << "Request completed" << std::endl;

QByteArray reponse = reply->readAll();
std::cout << reponse.toStdString() << std:: endl;

Could you let me know how to resolve this?

Cross origin error on browser

[Edited]: I tried to enable the node server for cros by using cors package. This enables the first OPTIONS call to succeed but the actual POST call that happens is send with incorrect payload.

OPTIONS

Post

0 Answers
Related