JavaScript怎么获取服务端返回的json?

JavaScript013

JavaScript怎么获取服务端返回的json?,第1张

用jquery的ajax组件可以很快实现js发送请求。参考以下JS代码:

<script>

$(function(){

var url = "http://abc.com"//你的请求url

//发起get请求

$.get(url, function(res){

console.log('ajax_get========', res)

},'json')

})

//发起post请求

var postdata = {

'id':'123'

}//post提交的参数

$.post(url, postdata, function(res){

console.log('ajax_get========', res)

},'json')

})

</script>

jquery 中有一个 load 可以加载远程的内容

<script type="text/javascript">

<!--$(document).ready(function() {

alert("")

$("#jsonid").load("https://api.coindesk.com/v1/bpi/currentprice/CNY.json",

function(data) {

alert(typeof(data))

})

})

//-->

</script>

jsonid 为一隐藏的容器 如 div span 等

然后你可以操作取回的数据 (上例中的data)

用 jQuery 吧:

$.ajax({

    url:'http://www.weather.com.cn/data/cityinfo/101010100.html',

    success:function( data ){

        console.log( data )

    }

})

然后就没有然后了