如何将JSON响应从VB传递到JS?
我正在更新一个通过JSON请求/响应接受付款的公司网站。它是用VB编写的,到目前为止,我已经能够成功地发送JSON请求并接收响应,但我的问题是将响应从VB传递到JS,以允许银行的外部JS文件将其支付页面注入到我的页面的DIV标记中。
我在这方面很新,尝试了很多事情,但没有运气。我得到的最接近的方法是将JSON响应传递给隐藏的输入字段,并让JS脚本通过document.getElementById获取值,但隐藏的输入字段只获取开头的{。
JSON响应的结构如下:
{"response" : {"success":"true","ticket":"random ticket number from bank"}}
下面是到目前为止的代码片段。抱歉,这里很乱。
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
Try
webClient.Headers("content-type") = "application/json"
reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(dictData, Formatting.Indented))
resByte = webClient.UploadData(Me.urlToPost, "post", reqString)
resString = Encoding.Default.GetString(resByte)
Dim sb As New System.Text.StringBuilder()
sb.Append(resString)
Dim JSONData As String = sb.ToString()
Dim p As Page = TryCast(HttpContext.Current.Handler, Page)
p.ClientScript.RegisterStartupScript(Me.GetType, "alert", JSONData)
System.Web.HttpContext.Current.Response.Write(String.Format("<input id=""jsonResponse"" value=""{0}"">", resString))
Dim jScript As String = "<script>var myCheckout = new bankCheckout();myCheckout.setMode('qa');myCheckout.setCheckoutDiv('bankCheckout');var form1Display = document.getElementById('form1');function showCheckout(){form1Display.style.visibility = 'hidden';bankCheckoutDisplay.style.visibility = 'visible';}var bankCheckoutDisplay = document.getElementById('bankCheckout');let result = document.querySelector('.result');var json = document.getElementById('jsonResponse');var obj = JSON.parse(json);alert(obj.response.ticket);myCheckout.startCheckout(obj.response.ticket);showCheckout();</script>"
p.ClientScript.RegisterStartupScript(Me.GetType, "Script", jScript, False)
webClient.Dispose()
Return True
End Try
1条答案
按热度按时间q43xntqr1#
您可以参考:https://github.com/beatum/demo-Json-manipulation-vbs