2011-03-15 46 views
1

我有一个数据存储,从我的服务器上的JSON api获取信息。当我在WebKit/Chrome中运行代码时,一切似乎都正常,但如果我将用户代理更改为iPhone 4.1等,则表明JSON请求未正确设置。与其发送获取JSON的请求,它只是发送请求并获取HTML呈现的页面。在SenchaTouch中为Ext.data.Store代理设置请求标头

这是我所定义的存储:

Product.ProductStore = new Ext.data.Store({ 
    model: 'Product', 
    proxy: { 
     type: 'ajax', 
     url: '/admin/products.json', 
     reader: { 
      type: 'json', 
      root: 'products' 
     } 
    }, 
    autoLoad: true, 
    storeId: 'productStore', 
    getGroupString: function(record){ 
     return record.get('vendor')[0]; 
    } 
}); 

所以,我真的希望有这家店做的是发送到/admin/products.json的请求,在桌面上按预期其中工程。但是,当我在我的模拟器或甚至在设备上运行它时,它似乎只是将请求发送到/ admin/products,而不是返回HTML。

有人提出这是一个rails服务器问题,我需要为我的请求设置内容类型。问题是我该如何做到这一点?

我试过以下,它似乎并没有工作,要么:

Product.ProductStore = new Ext.data.Store({ 
    model: 'Product', 
    proxy: { 
     type: 'ajax', 
     url: '/admin/products.json', 
      // Trying to set the headers for the request -- not working 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     reader: { 
      type: 'json', 
      root: 'products' 
     } 
    }, 
    autoLoad: true, 
    storeId: 'productStore', 
    getGroupString: function(record){ 
     return record.get('vendor')[0]; 
    } 
}); 

难道我只需更换用我自己的AJAX请求已设定正确的内容类型标头?如果是这样,是否有一些使用商店和它自己的自定义AJAX请求的例子?

回答

3

应该

'Accept' : 'application/json' 

接受的是客户端用来告诉它喜欢什么媒体类型的服务器头。 Content-Type是服务器发送的标题,作为响应的一部分,告诉客户端数据是什么。

+0

这似乎没有工作,我试着在我的代理对象中设置defaultHeaders和标题,但它仍然没有正确设置请求标题。 – csaunders 2011-03-15 15:55:43

+0

您是否用接受替换了Content-Type?你的回复不清楚。在Chrome中运行它时,请使用开发人员工具检查请求中的标题。 – 2011-03-15 18:03:17

+0

原来,这是一个服务器端的UA检测错误,总是将任何请求重定向到/ admin/products。对于含糊不清的道歉,我的意思是我的确尝试了你的建议。感谢您的帮助:) – csaunders 2011-03-15 19:03:42