我在我的 Qt MainWindow 中有一个作为 WebView 加载的 HTML 文件,该文件位于 localhost XAMPP 文件夹中。该文件基本上从用户那里获取两个日期和时间间隔,当用户单击“获取查询”按钮时,地图显示查询的总量 - 从 XAMPP 上的 MySQL 数据库获取 - 作为热图。但是,尝试从 Qt 运行此 HTML 文件 - 单击 HTML 窗口的 Fetch 查询按钮 - 给我 Ajax POST 错误,我无法从 Qt 访问数据库,而 localhost 副本可以。我还尝试发布到使用 NetworkAccessManager 获取查询的 PHP 文件,但我得到了一个“未知错误”作为回复。有没有一种特殊的方法来解决这个问题?HTML-jQuery 代码和 NetworkAccessManager 帖子代码如下:
HTML-jQuery 部分:
<form id="ajaxForm" action="index.php" method="post">
Start <input type="text" name = "date1" id = "datepicker" value = "2011-07-13" style = "width:70px">
<input type="text" name = "time1" id = "timepicker1" value = "00:00" style = "width:40px">
--
End <input type="text" name = "date2" id = "datepicker2" value = "2011-07-13" style = "width:70px">
<input type="text" name = "time2" id = "timepicker2" value = "00:01" style = "width:40px">
<select name = "freq">
<option value = "all" selected = "true">all</option>
<option value = "hourly">hourly</option>
<option value = "daily">daily</option>
<option value = "weekly">weekly</option>
<option value = "monthly">monthly</option>
</select>
data between
<input type="text" id="time" style="border:0; color:#f6931f; font-weight:bold;" />
<input type="submit" name="fetch" value="Fetch">
</form>
我用来测试我的 PHP 文件以获取回复的 NetworkAccessManager 部分:
QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);
QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
QUrl url = "http://localhost/heatQuery.php";
QUrl params;
params.addQueryItem("date1","2011-07-13");
params.addQueryItem("time1","00:00");
params.addQueryItem("date2","2011-07-13");
params.addQueryItem("time2","13:00");
params.addQueryItem("freq","hourly");
QByteArray data;
data.append(params.toString());
data.remove(0,1);
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant("application/x-www-form-urlencoded"));
mNetworkManager->post(request, data);
void MainWindow::replyFinished(QNetworkReply *reply){
//Use the reply as you wish
QString text(reply->errorString());
std::cout<<"REPLY IS" << text.toStdString() << std::endl;
}