这是可能通过JavaScript? [英] Is this possible through javascript?
问题描述
我目前工作的asp.net项目。我用八面Web服务。八面通过URL提供的结果。我使用<一个href=\"http://api.oodle.com/api/v2/listings?key=TEST®ion=chicago&category=vehicle&format=json\"相对=nofollow>链接从URL中获取数据。
I am currently working on asp.net project. I use oodle web service. Oodle provides result via URL. I use Link to get data from the URL.
从code:
string url =
@"http://api.oodle.com/api/v2/listings?
key=TEST®ion=chicago&category=vehicle&format=json";
var jsonString = new WebClient().DownloadString(url);
现在,我的问题是我使用按钮单击事件来运行code。但是,这是通过的JavaScript
?因为如果我使用的JavaScript
它会更容易访问我得到的数据。
Now , My problem is I use button click event to run that code. But is this through JavaScript
? Because If I use Javascript
it will be easier to access my resultant data.
推荐答案
它看起来像这样的API返回JSONP这意味着你可以直接从JavaScript使用它。例如,如果你使用jQuery,你可以使用 $。阿贾克斯()
方式:
It looks like this API returns JSONP which means that you could consume it directly from javascript. For example if you use jQuery you could use the $.ajax()
method:
$.ajax({
url: 'http://api.oodle.com/api/v2/listings?key=TEST®ion=chicago&category=vehicle&format=json',
type: 'GET',
dataType: 'jsonp',
jsonpCallback: 'jsonOodleApi',
success: function(result) {
alert(result.current.region.id);
}
});
下面是一个现场演示。
如果你不使用jQuery你可以简单地定义一个回调:
And if you don't use jQuery you could simply define a callback:
var jsonOodleApi = function(result) {
alert(result.current.region.id);
};
然后包括&LT;脚本&GT;
指向这个网址:
<script type="text/javascript" src="http://api.oodle.com/api/v2/listings?key=TEST®ion=chicago&category=vehicle&format=json"></script>
下面是一个现场演示。
显然,你可以,只要你要调用的API调用动态注入这个脚本标签到DOM。
Obviously you could inject this script tag dynamically into the DOM whenever you want to invoke the API call.
这篇关于这是可能通过JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!