首先,这是代码:
var requestAuthUri = 'https://github.com/login/oauth/access_token';
var ajaxArgs = {
type : 'POST',
url : requestAuthUri,
headers : {
'Accept' : 'application/json'
},
data : {
'client_id' : this.args['client_id'],
'client_secret' : this.args['client_secret'],
'code' : queryVars['code']
},
dataType : 'json',
error : function( jqXHR, textStatus, errorThrown ) {
console.log(errorThrown);
alert( textStatus + ' : ' + errorThrown );
}
};
console.log(ajaxArgs);
$.ajax( ajaxArgs ).done( function( response ) {
console.log( response );
});
这些是服务器回复标题:
HTTP/1.1 200 OK
Server: GitHub.com
Date: Mon, 01 Jul 2013 08:42:09 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
Cache-Control: private, max-age=0, must-revalidate
Strict-Transport-Security: max-age=2592000
X-Frame-Options: deny
Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Fri, 01-Jul-2033 08:42:09 GMT; HttpOnly
X-Runtime: 18
Etag: "c4d5365a37fa466698cb5dc6e66f61e3"
Vary: Accept-Encoding
Content-Encoding: gzip
这些是客户端标题:
POST /login/oauth/access_token HTTP/1.1
Host: github.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: application/json
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://site.it/app/?code=xxxxxxxxxxxx
Content-Length: 111
Origin: http://site.it
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
问题是身体内容是空的,$ .ajax触发了一个未识别的(空的errorThrown)错误,我正在努力搞清楚原因。
我已经尝试过的东西:
application/json
而不是application/x-www-form-urlencoded
我运气不好吗?! :)
答案 0 :(得分:0)
确保queryVars ['code']确实包含GitHub提供的正确代码。 然后,尝试使用text / plain作为响应,并跳过Accept-header,看看是否有任何响应。
您抛出的错误基本告诉您,您从服务器获得了格式错误的JSON响应。因此,首先尝试接收基本回复,然后继续以JSON格式获取它。