【问题标题】:Trouble understanding JavaScript API example with VB.NET无法理解带有 VB.NET 的 JavaScript API 示例
【发布时间】:2013-02-07 10:31:45
【问题描述】:

我是一个新的 vb.net 开发人员,我正在尝试与一个 API 服务集成,该服务允许我在不符合 PCI 的情况下存储信用卡数据。我遇到的问题是该示例仅适用于 JavaScript,我无法弄清楚如何在 vb.NET 中创建相同的简单过程。我希望有人可以帮助我制作这个样本。

想法是将这些数据从我的表单引导到 API,然后处理成功或失败的响应数据。

        //<![CDATA[
        $(document).ready(function() {
            $('input:button').click(function() {
                $.ajax({
                    type: 'post',
                    url: 'https://certify.i4go.com/index.cfm',
                    dataType: 'jsonp',
                    data: {
                        fuseaction: 'account.jsonpPostCardEntry',
                        i4Go_AccountID: '123123',
                        i4Go_SiteID: '456456',
                        i4Go_CardNumber: $('#i4Go_CardNumber').val(),
                        i4Go_ExpirationMonth: $('#i4Go_ExpirationMonth').val(),
                        i4Go_ExpirationYear: $('#i4Go_ExpirationYear').val()
                    },
                    cache: false
                });
            });                 
        });
        function parseResponse(data) {
            if (data.i4go_responsecode != null) {
                if (data.i4go_responsecode == 1) {
                    $('#response').html('i4Go_UniqueId: ' + data.i4go_uniqueid);
                } else {
                    $('#response').html('i4Go_ResponseCode: ' + data.i4go_responsecode);
                }
            } else {
                $('#response').html('i4Go_ResponseCode is null');
            }
        }
        //]]>
    </script>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tbody><tr>
            <td>Card number:</td><td><input type="text" id="i4Go_CardNumber" value="4111111111111111"></td>
        </tr>
        <tr>
            <td>Exp month:</td><td><input type="text" id="i4Go_ExpirationMonth" value="12"></td>
        </tr>
        <tr>
            <td>Exp year:</td><td><input type="text" id="i4Go_ExpirationYear" value="2020"></td>
        </tr>
        <tr>
            <td><input type="button" value="Get Token"></td>
        </tr>
    </tbody></table>
    <div id="response"></div>

感谢所有帮助。

干杯,

AJ

【问题讨论】:

    标签: javascript vb.net json asp.net-ajax jsonp


    【解决方案1】:

    如果您使用的是 .NET 4.5,请查看新的 WebAPI 调用,这些调用非常简单。一旦你创建了一个代表数据的类,序列化就为你完成了,你的代码看起来像这样:

    Dim jsonObject As ' TODO: Create a class that represents the data, and instantiate the object with the data
    Dim client As New HttpClient()
    client.BaseAddress = new Uri("https://certify.i4go.com/")
    Dim result As HttpResponseMessage = client.PostAsJsonAsync("index.cfm", jsonObject).Result
    

    分析返回的对象以获取它们传递给您的任何内容,并决定如何从那里继续。

    有很多重载可供选择,基本上都做同样的事情。

    编辑: 对于 .NET 4.0,您可以使用 WebClient 而不是 HttpClient - 这需要更多的工作,但仍然不算太糟糕 - 您只需手动处理序列化。可能还有很多方法可以做到这一点,但这里有一个:

    Dim message As String
    Dim s As New SomeData() ' SomeData still represents the data you're passing
    message = New JavaScriptSerializer().Serialize(s)
    Dim webClient As New WebClient()
    Dim response() As Byte = webClient.UploadData(url, Encoding.UTF8.GetBytes(message))
    Dim responseString as String = Encoding.UTF8.GetString(response)
    

    既然您将响应作为字符串,您可以弄清楚如何处理它 - 它可能是一个 JSON 对象或类似的东西,您需要读取或反序列化它。

    【讨论】:

    • 乔,谢谢你的帮助。我目前在 .NET 4.0 上,如果没有新的 HttpClient 我怎么能实现呢?
    • 用 4.0 示例编辑答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多