Qt WebView中出现cross domain ajax querying with jquery

来源:互联网 发布:sql replace函数用法 编辑:程序博客网 时间:2024/06/04 18:17

在Qt的WebView中,使用ajax请求会因为cross domain ajax querying with jquery而失败,此时可以将请求转化到WebView中,利用Qhttp发送请求,相当于模拟了一个Web服务器进行请求,可以避免出现这个问题,同时,在发起Qhttp请求时,一般都需要同步等待结果输出,可以利用QEventLoop和QTimer实现同步等待以及请求超时的需求。


QString SpriteDialog::update(const QString& urlString){QEventLoop loop;QTimer timer;QHttp req;QUrl url(urlString);req.setHost(url.host());QObject::connect(&req,SIGNAL(done(bool)),&loop,SLOT(quit()));QObject::connect(&timer,SIGNAL(timeout()),&loop,SLOT(quit()));timer.start(10000);req.get(url.path());qDebug()<<"request for "<<urlString;loop.exec();if (timer.isActive()){    timer.stop();    return QString(req.readAll());} else {    req.abort();    return QString("");}}


0 0