1

我在使用这个 AJAX 调用实际上返回任何类型的对象时遇到了麻烦。我知道我做错了什么,但我不知道在哪里。我希望有人可以帮助我,我希望在对象“zip”中返回一个元素。我真的很想得到任何回应,但我无法得到任何回报。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#submit').click(function() {
                var result = $('#resultDiv')
                $.ajax({
                    url: 'https://us-street.api.smartystreets.com/street-address',
                    method: 'get',
                    data: {
                        auth-id='your-auth-id',
                        auth-token='your-auth-token',
                        street=$('#street'),
                        city=$('#city'),
                        state=$('#state')
                    },
                    dataType: 'json',
                    success: function(data) {
                        if (data = null)
                        {
                          result.html('You failed');
                        }
                        else {
                          result.html('Match:' + data.components[0].zipcode)
                        }
                    }
                });
            });
        });
    </script>
    <title>SSTest</title>
</head>

<body>
    <div class="container">
        <h1 style="text-align:center"> Welcome to Address Check </h1>
        <form action="#" method="post">
            <div class="form-group">
                <label for="street">Street</label>
                <input type="text" id="street" class="form-control" name="street">
            </div>
            <div class="form-group">
                <label for="city">City</label>
                <input type="text" id="city" class="form-control" name="city">
            </div>
            <div class="form-group">
                <label for="state">State</label>
                <input type="text" id="state" class="form-control" name="state">
            </div>
            <button type="submit" id="submit" class="btn btn-default">Submit</button>
            <br/>
            <br/>
            <div id="resultDiv"></div>
</body>

</html>
4

2 回答 2

1

当您使用 GET 调用时,您可以先在浏览器中对此进行测试,并确保在开始将其包装在 JQuery 调用中之前得到响应。

https://us-street.api.smartystreets.com/street-address?auth-id=[your-auth-id]&auth-token=[your-auth-token]&street=SOMETHING&state=SOMETHING&city=SOMETHING

如果您得到一个非结果,那么请查阅 API 以查看您是否传递了正确的参数。

使用DOCS,此调用返回您的 API 密钥的数据 -

https://us-street.api.smartystreets.com/street-address?auth-id=[your-auth-id]&auth-token=[your-auth-token]&street=1600+amphitheatre+pkwy&city=mountain+view&state=CA&candidates=10

这个 JQuery Get HTML 示例得到响应 -

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.get("https://us-street.api.smartystreets.com/street-address?auth-id=[your-auth-id]&auth-token=[your-auth-token]&street=1600+amphitheatre+pkwy&city=mountain+view&state=CA&candidates=10", function(data, status){
            alert("zipcode: " + JSON.stringify(data));
        });
    });
});
</script>
</head>
<body>

<button>Send an HTTP GET request to a page and get the result back</button>

</body>
</html>

当您完善对 JQuery 的理解以获得所需的内容时,您应该能够从中构建。

于 2017-02-23T06:47:48.833 回答
0

我能够在您的代码中找到一些错误,并在此 JSFiddle中修复它们。这是您遇到的错误列表。

  1. 不要在公共代码中包含您的 auth-id、auth-token。通过这样做,您将放弃您的地址查找。您应该从您的帐户中删除这些并生成新的。

  2. 在您的原始success功能中,您没有进行比较。你应该==在这里使用。实际上,API 永远不会返回 null 来获取成功的数据,所以你甚至不再需要这个了。请改用该error功能。

  3. ajax 调用中传递的数据对象执行不正确。你不应该使用=,而是使用:.

  4. 在数据对象中,您应该.val()在 jQuery 选择器之后调用以获取输入到这些字段中的值。

  5. data.components[0].zipcode应该是data[0].components.zipcode。api 将返回一个对象数据数组。components不是数组。

于 2017-03-07T22:30:08.050 回答