I'm working on a simple web app. This will have the client enter a license plate, then connect to a Socrata-based OpenData API which contains the records of all registered cars in my country.
我正在开发一个简单的网络应用程序。这将让客户输入牌照,然后连接到基于Socrata的OpenData API,其中包含我所在国家/地区所有已注册汽车的记录。
Using the license plate, it must find the correct car, and display specific information. The kind of information displayed will differ, as I intend to use this app on several different web pages, but as a starting point I'd like to display the car's brand and model. These are listed in the array as
使用牌照,它必须找到正确的汽车,并显示具体信息。显示的信息类型会有所不同,因为我打算在几个不同的网页上使用这个应用程序,但作为一个起点,我想展示汽车的品牌和型号。这些在数组中列为
"merk" : "KIA"
"handelsbenaming" : "PICANTO"
Where 'Merk' is the brand/manufacturer and 'handelsbenaming' is the model.
'Merk'是品牌/制造商,'handelsbenaming'是模特。
I am not very experienced with javascript, and completely new to JQuery, but I managed to come up with this:
我对javascript不是很有经验,对JQuery来说是全新的,但我设法得到了这个:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
var kenteken = "1KBB00";
$(document).ready(function(){
$("button").click(function(){
$.get("https://opendata.rdw.nl/resource/m9d7-ebf2.json",{kenteken: kenteken},function(data, status){
alert("Data: " + data + " Status: " + status);});
});
});
</script>
</head>
<body>
<button>Click</button>
</body>
As you can see, the JQuery is fired by pushing the button. I'll make an input field to get the license plate number later on, but in for testing purposes I simply defined the variable "kenteken" to be a real license plate. Also for testing purposes I would like to return the response in an alert box.
如您所见,通过按下按钮可以触发JQuery。我将在后面输入一个输入字段来获取车牌号码,但为了测试目的,我只是将变量“kenteken”定义为真正的车牌。同样出于测试目的,我想在警告框中返回响应。
On testing, the console in Firefox does not indicate any errors. I can also see the GET request is made, and a JSON array is returned. However, I cannot get the array to write to the document or the alert box. Currently, the alert shows:
在测试时,Firefox中的控制台不会指出任何错误。我还可以看到GET请求,并返回一个JSON数组。但是,我无法让数组写入文档或警告框。目前,警报显示:
Data: [object Object] Status: success
Whereas, as far as I understand, 'Data' should display the entire JSON array which is returned by the GET request
然而,据我所知,'Data'应该显示GET请求返回的整个JSON数组
I used Google, read through the documentation that came with the API multiple times, I've read through more posts on SO than I can keep count of, but I'm not getting it.
我使用谷歌,多次阅读API附带的文档,我已阅读更多关于SO的帖子,但我不记得,但我没有得到它。
2 个解决方案
#1
1
The response is not a single object, it's an array of objects, so you need to index them to access the properties. I show below how to access the first one, but in your real code you would probably loop.
响应不是单个对象,它是一个对象数组,因此您需要索引它们以访问属性。我在下面展示如何访问第一个,但在您的真实代码中,您可能会循环。
$.getJSON("https://opendata.rdw.nl/resource/m9d7-ebf2.json",{kenteken: kenteken},function(data, status){
alert("Merk: " + data[0].merk + " Handelsbenaming: " + data[0]. handelsbenaming + " Status: " + status);});
});
#2
1
when you get your data back it comes as a string.
当你获得数据时,它就像一个字符串。
Turn it back into a JSON object using JSON.Parse(data) and the you should get it to look correctly. Put a breakpoint before your alert and then you can inspect what comes back and what the json method does.
使用JSON.Parse(数据)将其转回JSON对象,您应该让它看起来正确。在警报之前放置断点,然后您可以检查返回的内容以及json方法的作用。
$.get("https://opendata.rdw.nl/resource/m9d7-ebf2.json",{kenteken: kenteken},function(data, status){
debugger;
var jsonData = JSON.Parse(data);
alert("Data: " + jsonData + " Status: " + status);});
});
#1
1
The response is not a single object, it's an array of objects, so you need to index them to access the properties. I show below how to access the first one, but in your real code you would probably loop.
响应不是单个对象,它是一个对象数组,因此您需要索引它们以访问属性。我在下面展示如何访问第一个,但在您的真实代码中,您可能会循环。
$.getJSON("https://opendata.rdw.nl/resource/m9d7-ebf2.json",{kenteken: kenteken},function(data, status){
alert("Merk: " + data[0].merk + " Handelsbenaming: " + data[0]. handelsbenaming + " Status: " + status);});
});
#2
1
when you get your data back it comes as a string.
当你获得数据时,它就像一个字符串。
Turn it back into a JSON object using JSON.Parse(data) and the you should get it to look correctly. Put a breakpoint before your alert and then you can inspect what comes back and what the json method does.
使用JSON.Parse(数据)将其转回JSON对象,您应该让它看起来正确。在警报之前放置断点,然后您可以检查返回的内容以及json方法的作用。
$.get("https://opendata.rdw.nl/resource/m9d7-ebf2.json",{kenteken: kenteken},function(data, status){
debugger;
var jsonData = JSON.Parse(data);
alert("Data: " + jsonData + " Status: " + status);});
});