How can I transfer a JSON response from VB to JS?
I'm currently working on updating a company website that processes payments through JSON request/response communication. The site is built in VB, and while I've been able to successfully send the JSON request and receive a response, my challenge lies in passing this response from VB to JS so that the bank's external JS file can inject their payment page into a DIV tag on my page.
I am relatively new to this and have attempted various methods without success. The closest I have come is by transferring the JSON response to a hidden input field and using a JS script to retrieve the value with document.getElementById. However, the hidden input field only captures the opening { character.
The structure of the JSON response is as follows:
{"response" : {"success":"true","ticket":"random ticket number from bank"}}
Below is the current code snippet, apologies for its messiness.
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