2014-12-26 344 views
1

首先之间的差异,我真的不知道如何正确标题这一个或怎么形容“足够”的问题,使之可以理解的。HTTP请求的浏览器

不管怎样,我已经有了一个WordPress安装运行作为后端到博客。它安装了一个json-api-plugin。为此,我添加了一个基于Angular的前端,使用jQuery的$ .ajax()发送HTTP请求到后端。

现在,它运行在Chrome和Firefox(没试过IE浏览器,我在Mac上)完美的罚款。但在Safari中,我遇到了一些问题。看起来有些请求有效,而其他请求不会。

例如,http://api.walander.se/json/get_posts/?page=1要求完美的作品在所有三种浏览器(这也是唯一一个我已经得到了在Safari工作)。虽然,请求http://api.walander.se/json/get_category_index适用于Chrome和Firefox,但在Safari中失败。

在旅行检查工具给出的错误信息如下

[Error] Failed to load resource: Request header field Accept-Encoding is not allowed by Access-Control-Allow-Headers. (get_category_index, line 0) 
[Error] XMLHttpRequest cannot load http://api.walander.se/json/get_category_index/. Request header field Accept-Encoding is not allowed by Access-Control-Allow-Headers. (walander.se, line 0) 

因为在Safari中失败的所有请求给予同样的错误消息。

如果我从上面直接在Safari进入示例URL它的工作原理给出预期的反应。

{"status":"ok","count":2,"categories":[{"id":3,"slug":"development","title":"Development","description":"","parent":0,"post_count":1},{"id":2,"slug":"web","title":"Web","description":"","parent":0,"post_count":1}]} 

下面我介绍一些代码示例,可能有助于理解我的问题。

这是我的角RequestService处理所有HTTP的请求到服务器:

app.service('requestService',function() { 
    var API = 'http://api.walander.local/api/'; 

    this.send = function(type , service , dataIn , successCallback, errorCallback , loadingInfo) { 
     $.ajax({ 
      url: service, 
      dataType: 'json', 
      type: type, 
      data: dataIn, 
      timeout: 10000, 
      beforeSend: function() { 
       if (loadingInfo != undefined) 
        wLoading(loadingInfo.div,loadingInfo.text); 
      }, 
      success: function(data, textStatus, jqXHR) { 
       successCallback(data); 
      }, 
      error: function(data , jqXHR, textStatus, errorThrown) { 
       console.log("ERROR: Service failed, unable to get response"); 
       console.log("ERROR: textStatus: " + textStatus); 
       console.log("ERROR: errorThrown: " + errorThrown); 
       errorCallback(data); 
      }, 
      complete: function() { 
       if (loadingInfo != undefined) 
        wRemoveLoading(loadingInfo.div);  
      } 
     }); 
    } 
    this.get = function(service , dataIn , successCallback , errorCallback , loadingInfo) { 
     this.send('GET' , API+service , dataIn , successCallback , errorCallback , loadingInfo); 
    } 
    this.post = function(service , dataIn , successCallback , errorCallback , loadingInfo) { 
     this.send('POST' , API+service , dataIn , successCallback , errorCallback , loadingInfo); 
    } 
}); 

无论是例如URL:S的上方是GET型的,这意味着我使用reqeustService.get() -功能。

下面是我如何使用requestService请求当前页面的例子,从API分类列表:

app.service('blogService',function(requestService) { 
    this.getPage = function(page , refreshBlog) { 
     requestService.get('get_posts/?page='+page , null , function(data) { 
      handleResponse(data , refreshBlog , "page " + page); 
     } , function(data) {  

     } , { div: '#post-list' , text: 'Loading posts' }); 
    } 

    this.getCategories = function(refreshCategories) { 
     requestService.get('get_category_index', null , function(data) { 
      refreshCategories(data.categories); 
     } , function(data) { 

     } , { div: '#category-list', text: 'Loading categories' }); 
    } 
... (continued) ... 
}; 

不过,我不相信错误是在角码。我相信它与htaccess和HTTP请求头有关?

在此先感谢您的帮助!

+2

如果你使用angularjs - 使用它的$ http和$资源,而不是jQuery的ajax。我建议你用1个按钮来制作页面,这会产生有问题的请求(并且只使用角度,不使用jquery) - 那么你可以清楚地看到问题所在。 –

+0

感谢您的咨询!我将开始使用angular-js内置版本。不过,我不明白你的意思是什么按钮?这将如何澄清问题? – walander

回答

0

这是一个棘手的问题要解决。但我想我明白了。 =)

下面的错误消息的共振是您请求 get_categpory_index/您只接受以下编码:gzip,deflate,sdch。

[Error] Failed to load resource: Request header field Accept-Encoding is not allowed by Access-Control-Allow-Headers. (get_category_index, line 0) 

这是错误信息的原因,但解决这个问题并不能解决您的问题。


但是,为什么有些请求正在工作,有些不是?我在萤火虫做了一些调查,我发现你正试图取http://api.walander.se/json/get_category_index这是重定向到http://api.walander.se/json/get_category_index/。第二个请求有一组导致错误的标题。

的解决方案是在URL的末尾添加一个额外的斜杠,以避免重定向。

requestService.get('get_category_index/', null , function(data) { 
+0

非常感谢!这解决了我的问题! – walander