php解决方案
//环境感知API获取; function curlEntAPI($url, $data) { //headers头部设置; $headerArray = array("Content-type:application/json;charset='utf-8'", "USER-KEY:426aad8a150a4d85a8fa7221085edca3"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); curl_close($ch); return $output; //转为数组; //return json_decode($output, true); } $url = "http://iotb.***.com/service/page/sensor/simple.json?paged=1&pageSize=100"; //后台只接受字符串参数; $data = "{}"; print_r(curlEntAPI($url, $data));
ajax解决方案
一般出现400或415代码,表示链路是通的,一般是请求格式传递的参数出现错误导致的,确定data传递格式是字符串还是对象,data: JSON.stringify({})
即可。
$(function () { $.ajax({ type: 'post', url: "http://iotb.zhlead.com/service/page/sensor/simple.json?paged=1&pageSize=100", /*后台只接受JSON字符串格式参数*/ data: JSON.stringify({}), headers: { "USER-KEY": "426aad8a150a4d85a8fa7221085edca3", }, contentType: "application/json; charset=utf-8", dataType: "json", success: function (res) { console.log(res.data) }, error: function (err) { console.log(err) } }); })
lockdatav Done!