jQuery $ .ajax调用GitHub Oauth API返回一个未识别的错误

时间:2022-11-29 12:38:58

First of all this is the code:

首先这是代码:

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 );

    });

These are the server reply headers:

这些是服务器回复标题:

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

And these are the client headers:

这些是客户端标题:

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

The problem is that the body content is empty, an unindentified (empty errorThrown) error is triggered by $.ajax and i'm struggling figuring out why.

问题是身体内容是空的,$ .ajax触发了一个未识别的(空的errorThrown)错误,我正在努力搞清楚原因。

What i've allready tried:

我已经尝试过的东西:

  • changing from json to xml and text dataType
  • 从json更改为xml和text dataType

  • sending data as application/json instead of application/x-www-form-urlencoded
  • 将数据作为application / json而不是application / x-www-form-urlencoded发送

  • set crossDomain to true
  • 将crossDomain设置为true

Am i out of luck?! :)

我运气不好吗?! :)

1 个解决方案

#1


0  

Make sure that queryVars['code'] really holds the correct code given from GitHub. Then, try text/plain as response instead, and skip the Accept-header, just to see if you get any response.

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

The error thrown you get basicly tells you, that you've got an malformed JSON-response from the server. So try just to receive the basic reply at first, then move on to get it in JSON-format.

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

#1


0  

Make sure that queryVars['code'] really holds the correct code given from GitHub. Then, try text/plain as response instead, and skip the Accept-header, just to see if you get any response.

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

The error thrown you get basicly tells you, that you've got an malformed JSON-response from the server. So try just to receive the basic reply at first, then move on to get it in JSON-format.

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