web123456

ashx interface call_51.Qt-getting post data from ashx interface using ajax

Since current C++ projects require the use ofajaxThe library goes to post and calls the ashx interface at the following address.

The parameters to be passed are as follows.

Then found that qml is better to call the library, so this chapter through the C + + interface to get qml method to achieve the call ashx interface (to a C + + interface demo program as an example)

1. Grab post data

The post data fetched from the web page is as follows.

So when querying the period 20191121~20191122 then fill in the content: "deptCode=021&startDate=20191121&endDate=20191122"

2. Importing

The document reads as follows.

//GETfunctionget(url, success, failure)

{var xhr = newXMLHttpRequest;

("GET", url);

=function() {

handleResponse(xhr, success, failure);

}

();

}//POSTfunction post(url, arg, success, failure)

{var xhr = newXMLHttpRequest;

("POST", url);

("Content-Length", );

("Content-Type", "application/x-www-form-urlencoded;"); // Always have this when using POST = function() {

handleResponse(xhr, success, failure);

}

(arg);

}//Handling return valuesfunction handleResponse(xhr, success, failure){if ( ==) {if ( == 200){if (success != null && success !=undefined)

{var result =;try{

success(result, (result));

}catch(e){

success(result, {});

}

}

}else{if (failure != null && failure !=undefined)

failure(, );

}

}

}

3. Write

import QtQuick 2.3import QtQuick.Window2.2import"" asAjax

Item {function getWrenchTools(deptCode,startDate,endDate) {

console.log("Got message:", deptCode,startDate,endDate)//print parameter data

("http://10.194.102.253/WLPTService/Pages/Tools/","deptCode="+deptCode+"&startDate="+startDate+"&endDate="+endDate+"",

);

}

This means define a getWrenchTools() method, when the post succeeds and returns the data, then call the() callback.function (math.)(Widget: the C++ class corresponding to this qml, we will talk about how to bind it later)

The interface is as follows

Then write

#ifndef WIDGET_H#define WIDGET_H#include#include#include#include#include#include

namespaceUi {classwidget;

}class widget : publicQWidget

{

Q_OBJECT

QQmlApplicationEngine engine;

QObject*engineObject; //points to the running qml object

public:explicit widget(QWidget *parent = 0);~widget();private:

Ui::widget*ui;public:

Q_INVOKABLEvoidinvokeFunc(QVariant data1,QVariant data2);privateslots:voidon_pushButton_clicked();

};#endif //WIDGET_H

write

#include ""#include"ui_widget.h"#include#includewidget::widget(QWidget*parent) :

QWidget(parent),

ui(newUi::widget)

{

ui->setupUi(this);

()->setContextProperty("Widget",this);//Point the Widget variable in QML to the current class. This connects QML to the widget class.

engineObject= QQmlComponent(&engine, "qrc:/").create(); //create qml and get the running qml object

}

widget::~widget()

{deleteui;

}voidwidget::invokeFunc(QVariant data1,QVariant data2)

{

ui->plainTextEdit->setPlainText(());

}voidwidget::on_pushButton_clicked()

{

QVariant depatment= "021";

QVariant start= ui->start->text();

QVariant end= ui->end->text(); //"end date"

QMetaObject::invokeMethod(engineObject,"getWrenchTools",Q_ARG(QVariant, depatment)\

,Q_ARG(QVariant, start),Q_ARG(QVariant, end));

}

()-> setContextProperty("Widget", this) does.

Point the widget variable in QML to the current class. This connects the QML to the widget class, and then if the post succeeds, calls the invokeFunc(QVariant data1,QVariant data2) method of the current class to return the data.

When the synchronization button is pressed, on_pushButton_clicked() is called.

Since engineObject points to the running qml object, then we can easily request to call the getWrenchTools() function in the qml object through invokeMethod(). Thus, we can realize the post request

After clicking Synchronize, the result is as follows

Note - If QQmlComponent: Component is not ready field appears, it means that the path where qml and js are located is not in the interface folder.

When you create a file, you need to put it in the interface directory.