2012-03-23 69 views
5

我正在尝试通过javascript创建公开要点。我没有使用任何身份验证 - 这是所有客户端。无法发布到github v3 API

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

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

上述代码抛出400:错误的请求 - 解析JSON的问题。不过,我的JSON是有效的。有任何想法吗?

+1

你是从一个网站做到这一点。有可能的跨站点问题。 – MitMaro 2012-03-23 05:09:06

+1

大概应该说浏览器,而不是网站。 – MitMaro 2012-03-23 05:12:30

+0

有点像http://groups.google.com/group/helma/browse_thread/thread/3a89ec84a2815338,你检查了编码吗? – VonC 2012-03-23 05:14:54

回答

10

啊哈 - 我无法将对象传递给$ .post。它需要首先被串化:

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

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