【问题标题】:how do I prevent my json response from showing in the browser?如何防止我的 json 响应显示在浏览器中?
【发布时间】:2018-12-21 01:18:08
【问题描述】:

我正在使用来自 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 响应是在&lt;HTML&gt; 标记上方写入浏览器的第一件事。变量vAnswer 包含来自我成功接收的服务器的完整 JSON 响应。看起来是这样的

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

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

【问题讨论】:

  • json 被添加到哪里(在哪一行之间)?这是添加的vAnswer 还是qJSON.JSONOutput()
  • 您可能需要显示整个代码并检查包含的文件中是否有任何调试语句
  • 我添加了一些打印我的 sql 语句以进行测试的代码。在 response.write("
    ") 语句 @DanB
    之后,来自服务器的 JSON 响应正在写入页面
  • 我会检查包含并尝试添加更多代码。虽然它的页面很长。也许最好将其简化为基础知识并编辑问题。 @SearchAndResQ
  • json 写在&lt;html&gt; 标签之前。问题一定来自aspjson.asp

标签: json vbscript asp-classic


【解决方案1】:

谢谢@DanB 和@Lankymart

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

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

最令人尴尬的部分是,一年前我第一次开始使用此包含时,我在进行故障排除时可能自己这样做了。我不知道为什么我以前没有遇到过这个问题。

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2013-02-09
    相关资源
    最近更新 更多