3

我正在尝试通过 API 登录服务器,但我遇到了一个问题,将“dataType”设置为 JSON 会导致我收到“无响应正文”。

当 dataType 是 'json' 我收到这个回复:

Request URL:    http://104.236.232.23/api/user/login/?Email=haye123%40gmail.com&Password=password&_=1438891065386
Request Method:     GET
Status Code:    HTTP/1.1 200 OK
Request Headers 13:06:42.000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0
Origin: null
Host:   104.236.232.23
Connection: keep-alive
Cache-Control:  max-age=0
Accept-Language:    en-US,en;q=0.5
Accept-Encoding:    gzip, deflate
Accept: */*
Sent Cookie
sessionid:  2w5vttprgffpb9q9mu395ko1lfz3hv54
csrftoken:  NeEbmb3yKOpnb6IMLi5CCXKOYJwCe0ys
Response Headers Δ267ms
X-Frame-Options:    SAMEORIGIN
Vary:   Cookie
Server: nginx/1.4.6 (Ubuntu)
p3p:    CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"
Date:   Thu, 06 Aug 2015 20:06:43 GMT
Content-Type:   application/json
Content-Length: 385
Connection: keep-alive

但是,如果我将 dataType 更改为“脚本”,我会收到以下信息:

Request URL:    http://104.236.232.23/api/user/login/?Email=haye123%40gmail.com&Password=password&_=1438891656317
Request Method:     GET
Status Code:    HTTP/1.1 200 OK
Request Headers 13:07:36.000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0
Host:   104.236.232.23
Connection: keep-alive
Accept-Language:    en-US,en;q=0.5
Accept-Encoding:    gzip, deflate
Accept: */*
Sent Cookie
sessionid:  2w5vttprgffpb9q9mu395ko1lfz3hv54
csrftoken:  NeEbmb3yKOpnb6IMLi5CCXKOYJwCe0ys
Response Headers Δ123ms
X-Frame-Options:    SAMEORIGIN
Vary:   Cookie
Server: nginx/1.4.6 (Ubuntu)
p3p:    CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"
Date:   Thu, 06 Aug 2015 20:07:37 GMT
Content-Type:   application/json
Content-Length: 385
Connection: keep-alive
Response Body Δ0ms
{"last_name": "Name", "profile_image": "https://", "background_image": "", "is_staff": true, "id": 99, "date_joined": "01/09/2015", "first_name": "Aaron", "about_text": "", "gender": "", "age": null, "company_name": "0", "is_pro": false, "email": "email@gmail.com", "website_url": ""}

当 json 设置为 dataType 时,可能导致响应正文不显示的原因是什么?

以下是有问题的代码:

function Login2() {
  if( xhr != null ) {
    xhr.abort();
    xhr = null;
  }

  ajax_url = "http://104.236.232.23/api/user/login/";
  ajax_type = "GET";
  ajax_data = {'Email' : "email@gmail.com", 'Password' : "password" };

  xhr = $.ajax({
    url: ajax_url,
    type: ajax_type,
    data: ajax_data,
    crossDomain: true,
    dataType: 'json',
    xhrFields: {
      withCredentials: true
    },
    headers: {
      'Accept': '*/*'
    },
    success: function(data, textStatus, XMLHttpRequest){
      alert("boom");
      var cookietoSet=XMLHttpRequest.getResponseHeader('Set-Cookie');
      Set_Cookie(cookietoSet.split('=')[0],cookietoSet.split('=')[1],expires, path, domain, secure) //change as per ur needs
    }, //close success
    error: function( jqXHR, textStatus ){
      alert( "Request failed: " + textStatus );
    }
  });
} //end function
4

0 回答 0