拿到api数据接口该如何怎么写到自己的HTML里面

拿到api数据接口该如何怎么写到自己的HTML里面,第1张

推荐使用jquery,原生的ajax太繁琐

$.ajax({

url: '后台提供的Url',

type: "POST", //获取用get,提交的话用post

dataType: "json",

data:{

XXX:XXX //后台需要你传的数据

},

success: function (result) {

console.log(result) //这里就能打印出数据了

}

})

java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已给你两个方法吧:

public static String do_post(String url, List<NameValuePair>name_value_pair) throws IOException { String body = "{}"DefaultHttpClient httpclient = new DefaultHttpClient()try { HttpPost httpost = new HttpPost(url)httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8))HttpResponse response = httpclient.execute(httpost)HttpEntity entity = response.getEntity()body = EntityUtils.toString(entity)} finally { httpclient.getConnectionManager().shutdown()} return body} public static String do_get(String url) throws ClientProtocolException, IOException { String body = "{}"DefaultHttpClient httpclient = new DefaultHttpClient()try { HttpGet httpget = new HttpGet(url)HttpResponse response = httpclient.execute(httpget)HttpEntity ent发处篡肺诂镀磋僧单吉ity = response.getEntity()body = EntityUtils.toString(entity)} finally { httpclient.getConnectionManager().shutdown()} return body}

用json_decode()将json字符串转化成php数组,然后直接循环数组取出值即可。

<?php

$content= file_get_contents('获取json字符串的url')

$content = json_decode($content)//将json字符串转化成php数组

foreach ($content as $key ) {//循环数组

echo '<li>' . $key['city'] . '</li>'

echo '<li>' . $key['city_en'] . '</li>'

echo '<li>' . $key['date_y'] . '</li>'

echo '<li>' . $key['week'] . '</li>'

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7003861.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-31
下一篇 2023-03-31

发表评论

登录后才能评论

评论列表(0条)

保存