[英]Set value of apiDefaults to a variable
I have been trying to find a way to extract rows of data from an SQL database, format the returned rows into a JSON format and post/send that to a JSON web API. I am using an asp JSON serializer for VBScript (.Flush) to transform the SQL data into JSON. 它工作得很好,我有我需要的結構,我也有發送 JSON 格式數據的方法。 所以你可能會問什么問題......
問題是我無法將 apiDefaults.flush 發送到網絡服務器,它不起作用。 如果我發送與硬編碼變量相同的數據,它會完美運行,並且 API 會獲取數據。
讓我再解釋一下。
這是我使用 apiDefaults.flush 在屏幕上看到的 JSON output 這完全是我需要的格式。
{"authentication": { "apiKey": "123456789101112131415" }, "createProperties": [ { "name": "Property 1", "propKey": "6sf3zqq151n2wkah" }, { "name": "Property 2", "propKey": "yuf38zc4cls9hz19" } ] }
但是,我無法將此值存儲到變量中。 如果我可以簡單地執行以下操作,我的問題將得到解決,但您不能將 the.flush 方法綁定到變量??.! 我不明白為什么,因為理論上它只是一串文本。
dim myJson = apiDefaults.flush
這里的這段代碼是我設置要發送到 web 服務器的值的地方,我的變量包含 JSON 字符串
'IF I USE THIS IT WORKS PERFECTLY, IT TAKES MY MANUALLY CREATED VARIABLE/STRING AND SENDS IT
oXMLHttp.send strmultiprop
'THIS IS WHAT I FIGURED I SHOULD BE ABLE TO USE BUT IT IS NOT TREATED LIKE A VARIABLE
oXMLHttp.send apiDefaults.flush
' I TRIED THIS AND THIS WON'T WORK EITHER
oXMLHttp.send myJson
下面是我的實際腳本
<% @LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<!--#include file="JSON_2.0.4.asp"-->
<%
Function QueryToJSON(dbcomm, params)
Dim rs, jsa, aaa, bbb
Set rs = dbcomm.Execute(,params,1)
Set jsa = jsArray()
Do While Not (rs.EOF Or rs.BOF)
Set jsa(Null) = jsObject()
For Each col In rs.Fields
jsa(Null)(col.Name) = col.Value
Next
rs.MoveNext
Loop
Set QueryToJSON = jsa
rs.Close
End Function
strConn = "PROVIDER=SQLOLEDB;DATA SOURCE=LOCALHOST;UID=sa;PWD=********;DATABASE=DB_Site"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strConn
query = "SELECT wce_uid as name, uniqueid as propKey FROM wces_users WHERE Prop_Moved = ?"
CustomerID = "Y" 'Request.QueryString("CustomerID")
arParams = array(CustomerID)
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = query
Set cmd.ActiveConnection = conn
' https://stackoverflow.com/questions/40781130/setting-up-rest-api-in-classic-asp
key = "1234567891011121314151678910"
url = "https://www.beds24.com/api/json/createProperties"
'1 PROPERTY WORKING
str1prop = "{""authentication"": { ""apiKey"": ""123456789101112131415"" }, ""createProperties"": [ { ""name"": ""New Test"", ""propKey"": ""NewTest1"" } ] }"
'2 PROPERTY WORKING
strmultiprop = "{""authentication"": { ""apiKey"": ""123456789101112131415"" }, ""createProperties"": [ { ""name"": ""3333"", ""propKey"": ""3333"" }, { ""name"": ""4444"", ""propKey"": ""4444"" } ] }"
set apiDefaults = QueryToJSON(cmd, arParams)
'3 THE FLUSH METHOD --- THIS DOES NOT SET THE VRIABLE TO THE OUTPUT I SEE ON SCREEN
dim myJson = apiDefaults.flush
apiDefaults.flush
conn.Close : Set Conn = Nothing
Dim oXMLHttp
Set oXMLHttp=Server.Createobject("MSXML2.ServerXMLHTTP.6.0")
oXMLHttp.open "POST", url,false
oXMLHttp.setRequestHeader "Content-Type", "application/json"
oXMLHttp.send apiDefaults.flush 'THIS IS THE ISSUE LINE USING THIS. if I CHANGE TO strmultiprop NO ISSUES AT ALL.
response.write oXMLHttp.responseText
Set oXMLHttp = Nothing
%>
對此的任何建議都會很棒。
感謝您的關注。
@SearchAndResQ 回答了我的問題
“嘗試 oXMLHttp.send apiDefaults.jsString,您需要該屬性中的值”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.