如何防止我的 json 响应显示在浏览器中?

how do I prevent my json response from showing in the browser?

我正在使用 aspjson.com 中的 aspjson 来解析和编写经典 ASP 中的 json 以与 Authorize.net 进行交互。我让它工作,但出了点问题。来自服务器的响应被写入浏览器并被插入到我的表单字段中(如预期的那样)。我该如何防止这种情况?

我正在使用的包含可以在 http://www.aspjson.com/

找到

我觉得跟响应类型有关系,但是不知道怎么设置才正确。我试过 xmlhttp.responseType = "json" 但它阻止了我的令牌填充我的表单元素。我也尝试将我的变量设置为 xmlhttp.response,但它似乎没有什么不同。

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="aspjson.asp" -->

<%

dim vURL

vURL ="https://apitest.authorize.net/xml/v1/request.api"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
xmlhttp.open "POST", vURL, false 
xmlhttp.setRequestHeader "Content-type","application/json"
xmlhttp.setRequestHeader "Accept","application/json"
xmlhttp.send "{""getHostedPaymentPageRequest"": { ""merchantAuthentication"": [{ ""name"": ""CorrectUname"", ""transactionKey"": ""correctKey"" }], ""transactionRequest"": { ""transactionType"": ""authCaptureTransaction"", ""amount"": ""1250.00"", ""profile"": { ""customerProfileId"": ""123456789"" }, ""order"" : {""invoiceNumber"": ""0987654322"", ""description"" : ""Materials""}, ""lineItems"" : {""lineItem"" :{""itemId"" : ""1"", ""name"" : ""Guide"", ""description"":""A description of the item."", ""quantity"" : ""5"", ""unitPrice"" : ""150.00"", ""taxable"" : ""false""},""lineItem"" :{""itemId"" : ""2"", ""name"" : ""Guide PDF"", ""description"":""A description of the item."", ""quantity"" : ""5"",   ""unitPrice"" : ""100.00"", ""taxable"" : ""false""}},""customer"": { ""email"": ""user@example.com"" }, ""billTo"": { ""firstName"": ""Ellen"", ""lastName"": ""Johnson"", ""company"": ""Souveniropolis"", ""address"": ""14 Main Street"", ""city"": ""Pecan Springs"", ""state"": ""TX"", ""zip"": ""44628"", ""country"": ""USA"" }, ""shipTo"": { ""firstName"": ""Ellen"", ""lastName"": ""Johnson"", ""company"": ""Souveniropolis"", ""address"": ""14 Main Street"", ""city"": ""Pecan Springs"", ""state"": ""TX"", ""zip"": ""44628"", ""country"": ""USA"" } }, ""hostedPaymentSettings"": { ""setting"": [{ ""settingName"": ""hostedPaymentReturnOptions"", ""settingValue"": ""{\""showReceipt\"": true, \""url\"": \""https://example.com/reciept.asp\"", \""urlText\"": \""Continue\"", \""cancelUrl\"": \""https://example.com/cancel\"", \""cancelUrlText\"": \""Cancel\""}"" }, { ""settingName"": ""hostedPaymentButtonOptions"", ""settingValue"": ""{\""text\"": \""Pay\""}"" }, { ""settingName"": ""hostedPaymentStyleOptions"", ""settingValue"": ""{\""bgColor\"": \""blue\""}"" }, { ""settingName"": ""hostedPaymentPaymentOptions"", ""settingValue"": ""{\""cardCodeRequired\"": false, \""showCreditCard\"": true, \""showBankAccount\"": true}"" }, { ""settingName"": ""hostedPaymentSecurityOptions"", ""settingValue"": ""{\""captcha\"": false}"" }, { ""settingName"": ""hostedPaymentShippingAddressOptions"", ""settingValue"": ""{\""show\"": false, \""required\"": false}"" }, { ""settingName"": ""hostedPaymentBillingAddressOptions"", ""settingValue"": ""{\""show\"": true, \""required\"": false}"" }, { ""settingName"": ""hostedPaymentCustomerOptions"", ""settingValue"": ""{\""showEmail\"": true, \""requiredEmail\"": true, \""addPaymentProfile\"": true}"" }, { ""settingName"": ""hostedPaymentOrderOptions"", ""settingValue"": ""{\""show\"": true, \""merchantName\"": \""name\""}"" }, { ""settingName"": ""hostedPaymentIFrameCommunicatorUrl"", ""settingValue"": ""{\""url\"": \""https://example.com/special\""}"" }] } } }"

vAnswer = xmlhttp.responseText  
Set oJSON = New aspJSON
'Load JSON string - This uses the aspjson.asp library loaded above to process the json response
oJSON.loadJSON(vAnswer)
vToken = oJSON.data("token")

%>

<HTML>
<HEAD>
<TITLE> Authorize.net Test Page - danielso Sandbox account</TITLE>
</HEAD>
<BODY>
<h1>Authorize.net Test Page - danielso Sandbox account</h1>
<hr />

<form id="send_hptoken" action="https://test.authorize.net/payment/payment" method="post" target="load_payment" >
<input type="text" name="token" value="<%response.write(vToken)%>" />
<input type = "submit" value = "Order Now!" /> 
</form> 

问题是 JSON 响应是在 <HTML> 标记上方写入浏览器的第一件事。变量 vAnswer 包含我成功接收到的来自服务器的完整 JSON 响应。看起来像这样

{"token":"1Zxb060yfEUSZpUT6X0PPv...superLongButWorkingToken...Mvrbg.2GgexrHg74XQ","messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}

响应已解析,我需要的令牌包含在变量 vToken 中,该变量已成功填充到我的表单字段中。

谢谢@DanB 和@Lankymart

包含文件 aspjson.asp 确实被编辑为在 loadJSON() 方法的开头包含一个 response.write() 命令。

...
Public Sub loadJSON(inputsource)
    response.write inputsource
...

最令人尴尬的部分是,当我在一年前第一次开始使用这个 include 时,我在排除故障时可能是我自己做的。我不确定为什么我之前没有 运行 解决这个问题。