1

首先这是代码:

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)错误,我正在努力找出原因。

我已经尝试过的:

  • 从 json 更改为 xml 和 text dataType
  • 发送数据application/json而不是application/x-www-form-urlencoded
  • 将 crossDomain 设置为 true

我运气不好?!:)

4

1 回答 1

0

确保 queryVars['code'] 确实包含 GitHub 提供的正确代码。然后,尝试使用 text/plain 作为响应,并跳过 Accept-header,看看您是否得到任何响应。

您收到的错误基本上告诉您,您从服务器收到了格式错误的 JSON 响应。因此,首先尝试接收基本回复,然后继续以 JSON 格式获取它。

于 2013-07-01T09:16:42.747 回答