docs/en/guide/wxopen/request-service.md
The applet client can use Ajax-like functionality to send requests to the server and get the response information (usually JSON).
After completing the basic preparations for [client-side development], on the page (e.g. /pages/index/index.wxml), create a button for triggering the request:
<button
type="primary"
bindtap="doRequest"
hover-class="other-button-hover"
class="btn-DoRequest"
>
Getting data
</button>
In the above code, type, class and hover-class set the type, regular style and click style of the button respectively, and bindtap="doRequest" specifies that after clicking, it will be handled by the doRequest() method (function).
Reference documentation for this project:
Senparc.Weixin.WxOpen.AppDemo/pages/index/index.wxml
The doRequest() method is written in the index.js file:
// Handling wx.request requests
doRequest:function(){
var that = this;
wx.request({
url: wx.getStorageSync('domainName') + '/WxOpen/RequestData',
data: { nickName : that.data.userInfo.nickName},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: { 'content-type':'application/x-www-form-urlencoded'},
success: function(res){
// success
var json = res.data;
// Modal dialogue
wx.showModal({
title: 'Received Message',
content: json.msg, showCancel:false,
showCancel:false,
success: function(res) {
if (res.confirm) {
console.log('User clicked OK')
}
}
});
},
fail: function() {
// fail
},
complete: function() {
// complete
}
})
},
In the above code
wx.request(...) method is used to make a request to the server side, similar to axios.get() / .post() or $.ajax() in other JS frameworks.url is used to specify the API address to be sent. Where wx.getStorageSync('domainName') is used to flexibly specify the domain name of the development or production environment (see the app.js file in this directory).userInfo.nickName from the local data centre, when the user is logged in, we can get the WeChat nickname, otherwise it is undefined. 4.header is a parameter in the current request header, which can usually also be used to provide user authentication tokens in JWT mode. 6.success is the callback after a successful response (200) to the current request. the sample code shows that after receiving the success message a modal dialogue box will pop up to show the return content. click the [OK] button. the console will output the relevant logs.Reference documentation for this project:
Senparc.Weixin.WxOpen.AppDemo/pages/index/index.js
The server side can use various methods that can accept requests, such as pages, Actions in MVC, Web Api, middleware, and even aspx, ashx, and so on. The above request address is used in the local environment.
The above request address for local environment is https://localhost:44367/WxOpen/RequestData, we create an Action of RequestData under WxOpenController to receive the request:
[HttpPost]
public ActionResult RequestData(string nickName)
{
var data = new
{
msg = string.Format("Server time: {0}, Nickname: {1}", SystemTime.Now.LocalDateTime, nickName)
};
return Json(data);
}
Reference file for this project:
/Controllers/WxOpenController.cs
Click the [Get Data] button:
Since you are not currently logged in, nicknames are not available, so you can display them after logging in.
Tip: For login operations, see the [Login] tab.
Click the [OK] button: