|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 lon91ong 于 2019-12-3 14:21 编辑
问题描述:
问题代码:- Public Function httpReq(dataStr As String) As String Dim serverHost As String
- serverHost = "http://127.0.0.1:8020/test"
- Dim fetch As Object
- Set fetch = CreateObject("MSXML2.XMLHTTP")
- fetch.Open "POST", serverHost, False
- fetch.SetRequestHeader "Client", "Excel"
- fetch.Send dataStr '此处提示出错
- httpReq = fetch.ResponseText
- End Function
- Sub upData()
- Dim RowUsed As Integer, ColumnScore As String
- Dim cellScore As Range
- Dim dataStr As String, resp As Object
- dataStr = "["
- For Each cellScore In Range("A5:A26") '串接成绩字符串
- If Not cellScore.Value = vbNullString Then
- dataStr = dataStr & "{""score"":" & CStr(cellScore.Value) & "},"
- End If
- Next
- dataStr = JsonConverter.ConvertToJson(Left(dataStr, Len(dataStr) - 1) & "]")
- Set resp = JsonConverter.ParseJson(httpReq(dataStr))
- If resp("success") Then
- MsgBox resp("Content"), vbInformation, "完成"
- Else
- MsgBox "更新成绩失败!!!", vbInformation, "错误"
- End If
- End Sub
复制代码 函数功能描述:
其中JsonConverter是用来格式化字符串为Json的工具
函数httpReq接收字符串参数作为POST的数据
模块upData把成绩数据串接起来用JsonConverter格式化为Json格式,调用HttpReq接收返回值
问题描述:
Json数据格式化的步骤放在updata中,上面的样子就会报错,Win7下提示Send参数错误
(Win7 + WPS专业版报错;Win10 + WPS专业版 + Excle2016无错)
如果把Json格式化的步骤放到httpReq中,像下面就无错:
- fetch.Send JsonConverter.ConvertToJson(dataStr)
复制代码
区别仅仅是格式化Json和调用httpReq的前后顺序
请大侠指点一下错误的原因
补充:本地测试服务器python脚本:- import falconfrom json import dumps,loads
- from waitress import serve
- class Resource(object):
- def on_post(self, req, resp):
- print(req.url)
- scores = loads(req.media)
- print('更新{}条记录!'.format(len(scores)))
- resp.body = dumps({"success": True,"Content":'更新{}条记录!'.format(len(scores))},ensure_ascii=False)
- resp.status = falcon.HTTP_201
- app = falcon.API()
- app.add_route('/test', Resource())
- serve(app, listen='127.0.0.1:8020')
复制代码
没有python环境的,可以下载可执行文件(pyinstaller打包,可能被360报马)
微云下载:https://share.weiyun.com/5zaXqV3
|
|