我在使用model.save
時遇到了問題,因爲我的跨域請求無法返回cookie,但Ext.Ajax.request
沒有問題發回cookies。在我們的本地開發工作中,我們覆蓋了對所有Ajax請求(其中包括模型操作)的beforerequest,以設置withCredentials = true
和useDefaultXhrHeader = false
,這有助於在正常的Ext.Ajax.request上使用CORS。Ext.Ajax.request vs model.save跨源數據庫
據我所知,ajax
類型的代理類似於做Ext.Ajax.request,這很明顯,因爲model.save碰到beforerequest(你會在下面的例子中看到這個)。在這個例子中,你還會注意到model.save首先發送一個OPTIONS調用,然後永遠不會跟進POST(但這很可能是因爲服務器)......無論哪種方式,OPTIONS而不是POST似乎有點兒腥。
這裏的example:
Ext.Ajax.on({
beforerequest: function (conn, options, eOpts) {
console.log('here', conn, options)
options.useDefaultXhrHeader = false;
options.withCredentials = true;
}
});
Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: ['test'],
proxy: {
type: 'ajax',
url: 'https://docs.sencha.com'
}
});
var model = Ext.create('MyModel');
// This will fire off an OPTIONS call and not send the cookies
model.save();
// This will fire off with the POST and send the cookies
Ext.Ajax.request({
url: 'https://docs.sencha.com',
method: 'POST'
});
有沒有什麼辦法讓model.save到Cookies發送到跨域?
我不明白爲什麼我們不能讓這種行爲像一個正常的Ajax請求......它清楚地使用普通Ajax請求的部分,那麼到底是什麼使這個循規蹈矩不同?這是有效載荷的處理方式嗎?我們不能將模型的標題更改爲表單數據嗎? – incutonez