您当前的位置: 首页 >  ajax

漏刻有时

暂无认证

  • 0浏览

    0关注

    717博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ajax获取API数据出现400或415的解决方案

漏刻有时 发布时间:2021-09-02 16:51:41 ,浏览量:0

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!

关注
打赏
1661217259
查看更多评论
立即登录/注册

微信扫码登录

0.0403s