我在使用这个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>发布于 2017-02-23 14:47:49
当您使用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-03-08 06:30:08
我在你的代码中发现了一些错误,并在this JSFiddle中修复了它们。以下是您遇到的错误列表。
success函数中,你没有进行比较。您应该在这里使用==。实际上,API永远不会在成功时返回null数据,因此您在这里甚至不再需要它。使用error函数。=,而应该使用:..val(),以获得输入到这些fields.data.components[0].zipcode中的值应该为data[0].components.zipcode。api将返回对象的数据数组。components不是数组。https://stackoverflow.com/questions/42408434
复制相似问题