You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Dropbox create_shared_link_with_settings接口报400 JSON解码错误

核心错误原因
  • data字段格式错误:你将完整的JSON参数字符串作为JS对象的键、值设为空字符串,完全不符合Dropbox API要求的请求体格式
  • 缺少processData: false配置:jQuery默认会对传入的data做表单格式转换,当你传JSON字符串时必须关闭该逻辑,否则请求体内容会被篡改
修正后的完整代码
<!doctype html>
<html>
<head>
</head>
<body>
    <form id="form">
        <h3>选择文件并上传</h3>
        <input type="file" id="file">
    </form>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script>
        var input = document.getElementById("file");

        //在文件选择对话框选中文件时
        input.addEventListener("change", function (evt) {
            //Dropbox的文件API
            const reqConfig = {
                "url": 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings',
                "type": 'post',
                // 修正:直接传入JS参数对象转JSON字符串
                "data": JSON.stringify({
                    "path": "/hand4.jpg",
                    "settings": {
                        "audience": "public",
                        "access": "viewer",
                        "requested_visibility": "public",
                        "allow_download": true
                    }
                }),
                "headers": {
                    "Authorization": "Bearer *********", // 替换为你自己的有效AccessToken
                    "Content-Type": "application/json"
                },
                // 新增:禁止jQuery自动处理data格式
                processData: false,
                success: function (data) {
                    console.log(data);
                },
                error: function (data) {
                    console.error(data);
                }
            }
            $.ajax(reqConfig)
        })
    </script>
</body>
</html>
额外注意事项
  • 请将Authorization头里的*********替换为你自己的有效Dropbox访问令牌
  • path参数需要和你Dropbox里的文件实际路径完全匹配,如果你是要获取刚上传文件的共享链接,需要先完成文件上传逻辑拿到返回的文件路径,再调用该接口

内容的提问来源于stack exchange,提问作者肬黒竜太

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 22:15:05