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

时间:2018-12-21 01:18:08

标签: json vbscript asp-classic

我正在使用来自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中,该变量已成功填充到我的表单字段中。

1 个答案:

答案 0 :(得分:0)

谢谢@DanB和@Lankymart

确实已经编辑了包含文件aspjson.asp,使其在<form action="" method="POST" role="form" class="form-horizontal" enctype="multipart/form-data"> {{ form.csrf_token() }} <div class="form-group"> <label class="control-label col-sm-3">Firma</label> <div class="col-sm-6"> {% if action=='create' %} <!-- Other fields --> {{ form.signature(placeholder="Firma", type="file", class="form-control", accept="image/*", required="True") }} <img id="firmaimg" src="{{ url_for('static', filename='img/firma130x50_dummy.png') }}" alt="Firma" /> {{ form.save(class="btn btn-success btn-md") }} {% endif %} </div> </div> </form> 方法的开头包含一个response.write()命令。

loadJSON()

最令人尴尬的部分是,大约一年前,当我首次开始使用此功能时,我在进行故障排除时很可能自己做了此操作。我不确定为什么以前没有遇到这个问题。