doumengyin0491 2015-03-02 04:42
浏览 129
已采纳

Ajax CORS标头错误

I'm really struggling to figure out why this CORS request is getting denied. It's been driving me crazy. It gives an error "NetworkError: 500 Internal Server Error - http://api.example.com/v1/users/me" and then a message about cross-domain/CORS in Firebug.

JQuery

jQuery(document).ready(function(){

        var settings = {
            headers: {
                'Authorization': 'Token token= 12345'
            },
            type: 'GET',
            dataType: 'json'
        };

        var deferred = jQuery.ajax('http://api.example.com/v1/users/me', settings).then(
            function(data) {
                console.log(data);            
            }, function(error) {
                console.log(error);
            }
        );

});

Request Headers

OPTIONS /v1/users/me HTTP/1.1
Host: api.example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://fiddle.jshell.net
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
Connection: keep-alive

Response Headers

HTTP/1.0 500 Internal Server Error
Date: Mon, 02 Mar 2015 04:28:29 GMT
Server: Apache/2.2.22
X-Powered-By: PHP/5.5.21
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Access-Control-Allow-Origin: http://fiddle.jshell.net
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE
Access-Control-Allow-Headers: authorization
Set-Cookie: PHPSESSID=dfa4a0638bcf104d316f7358153bb2f9; path=/
Vary: User-Agent
Content-Type: application/json
X-Cache: MISS from 172.19.134.2
X-Cache-Lookup: MISS from 172.19.134.2:3128
Via: 1.0 172.19.134.2:3128 (squid/2.6.STABLE14)
Connection: close

If anyone is interested, I have this in PHP:

PHP

// Set Headers
header('Access-Control-Allow-Origin: http://fiddle.jshell.net');
header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE');
header('Access-Control-Allow-Headers: authorization');      
header('Content-Type: application/json; charset=utf-8');    
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥50 需要加权最小二乘Python代码
      • ¥15 hanzi-writer生成的svg外有一层div,修改div宽高影响的是画布大小,字体在不同视口下完全不相同。我想要让字体跟画布同比例大小该怎么实现?
      • ¥15 Java集合多对一问题集
      • ¥20 PowerShell如何操作记事本?
      • ¥15 intel网卡固件降级失败
      • ¥20 html使用模型的问题
      • ¥15 这些怎么解题啊来个人
      • ¥15 h5套壳IOS Cordova依赖找不到导致编译失败
      • ¥15 分组密码算法misty1的子密钥生成算法c++实现
      • ¥15 vscode的一些报错,希望可以得到解决