5

我正在尝试通过 javascript 创建一个公共要点。我没有使用任何身份验证 - 这都是客户端。

var gist = {
    "description": "test",
    "public": true,
    "files": {
        "test.txt": {
            "content": "contents"
        }
    }
};

$.post('https://api.github.com/gists', gist, function(data) {
});

上面的代码抛出 400: Bad Request - Problems parsing JSON。但是,我的 JSON 是有效的。有任何想法吗?

4

1 回答 1

10

啊哈 - 我无法将对象传递给 $.post。首先需要对其进行字符串化:

var gist = {
    "description": "test",
    "public": true,
    "files": {
        "test.txt": {
            "content": "contents"
        }
    }
};

$.post('https://api.github.com/gists', JSON.stringify(gist), function(data) {});
于 2012-03-23T15:26:02.267 回答