51.Qt-使用ajax獲取ashx介面的post資料
阿新 • • 發佈:2019-12-24
由於當前C++專案需要使用ajax庫去post呼叫ashx介面,介面地址如下所示:
需要傳遞的引數如下:
然後發現qml比較好呼叫ajax.js庫,所以本章通過C++介面去獲取qml方法來實現呼叫ashx介面(以一個C++介面demo程式為例)
1.抓post資料
通過網頁獲取到的post資料如下所示:
所以查詢20191121~20191122期間時則填入內容: "deptCode=021&startDate=20191121&endDate=20191122"
2.匯入ajax.js庫
ajax.js檔案如下所示:
// GET function get(url, success, failure) { var xhr = new XMLHttpRequest; xhr.open("GET", url); xhr.onreadystatechange = function() { handleResponse(xhr, success, failure); } xhr.send(); } // POST function post(url, arg, success, failure) { var xhr = new XMLHttpRequest; xhr.open("POST", url); xhr.setRequestHeader("Content-Length", arg.length); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //用POST的時候一定要有這句 xhr.onreadystatechange = function() { handleResponse(xhr, success, failure); } xhr.send(arg); } // 處理返回值 function handleResponse(xhr, success, failure){ if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.status == 200){ if (success != null && success != undefined) { var result = xhr.responseText; try{ success(result, JSON.parse(result)); }catch(e){ success(result, {}); } } } else{ if (failure != null && failure != undefined) failure(xhr.responseText, xhr.status); } } }
3.寫main.qml
import QtQuick 2.3 import QtQuick.Window 2.2 import "ajax.js" as Ajax Item { function getWrenchTools(deptCode,startDate,endDate) { console.log("Got message:", deptCode,startDate,endDate) //列印引數資料 Ajax.post("http://10.194.102.253/WLPTService/Pages/Tools/GetNLToolsByDeptCode.ashx","deptCode="+deptCode+"&startDate="+startDate+"&endDate="+endDate+"", Widget.invokeFunc); }
這裡表示定義一個getWrenchTools()方法,當post成功並返回資料時,則呼叫Widget.invokeFunc()回撥函式(Widget: 該qml對應的C++類,後面會講怎麼捆綁的)
4.widget介面如下
然後寫widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QString> #include <QDebug> #include <QTimer> #include <QQmlApplicationEngine> #include <QQmlComponent> namespace Ui { class widget; } class widget : public QWidget { Q_OBJECT QQmlApplicationEngine engine; QObject *engineObject; //指向執行的qml物件 public: explicit widget(QWidget *parent = 0); ~widget(); private: Ui::widget *ui; public: Q_INVOKABLE void invokeFunc(QVariant data1,QVariant data2); private slots: void on_pushButton_clicked(); }; #endif // WIDGET_H
寫widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QTimer> #include <QQmlContext> widget::widget(QWidget *parent) : QWidget(parent), ui(new Ui::widget) { ui->setupUi(this); engine.rootContext()->setContextProperty("Widget",this); //將QML中的Widget變數指向為當前類.從而使QML和widget類連線起來 engineObject = QQmlComponent(&engine, "qrc:/main.qml").create(); //建立qml並獲取執行中的qml物件 } widget::~widget() { delete ui; } void widget::invokeFunc(QVariant data1,QVariant data2) { ui->plainTextEdit->setPlainText(data1.toString()); } void widget::on_pushButton_clicked() { QVariant depatment= "021"; QVariant start= ui->start->text(); QVariant end = ui->end->text(); //"結束日期" QMetaObject::invokeMethod(engineObject, "getWrenchTools",Q_ARG(QVariant, depatment)\ ,Q_ARG(QVariant, start),Q_ARG(QVariant, end)); }
- engine.rootContext()->setContextProperty("Widget",this)的作用:
將QML中的Widget變數指向為當前類.從而使QML和widget類連線起來, 然後main.qml如果post成功則呼叫當前類的invokeFunc(QVariant data1,QVariant data2)方法,從而實現資料返回.
- 當按下同步按鈕時,則呼叫on_pushButton_clicked():
由於engineObject指向執行中的qml物件,然後我們通過invokeMethod()就可以方便的請求呼叫qml物件中的getWrenchTools()函式.從而實現post請求
點選同步後,效果如下所示(然後可以參考50.Qt-QJsonDocument讀寫json來提取資料):
&n