我正在开发一款使用Backbone JS的phonegap应用。 在ajax调用期间,标题包含:
“原点”: “文件://”
服务器不支持。我尝试将 Origin 标头设置为 null ,但在Chrome中不允许这样做。
SELECT item_id, share_by_id, share_to_id
引发错误:
拒绝设置不安全的标题“Origin”
只有解决这个问题才能解决这个问题就是使用cordovaHttp插件。但我无法弄清楚如何覆盖Backbone.ajax以使用cordovHTTP。
指向cordova插件的链接: $parse and $interpolate
虽然这与CORS有关,但我的问题是使用cordovaHttpPlugin覆盖Backbone ajax方法
答案 0 :(得分:0)
有效:
function isPhoneGap() {
return (window.cordova || window.PhoneGap || window.phonegap)
&& /^file:\/{3}[^\/]/i.test(window.location.href)
&& /ios|iphone|ipod|ipad|android/i.test(navigator.userAgent);
}
Backbone.sync = function( method, model, options ) {
if(method == "read"){
if(isPhoneGap()){
cordova.plugin.http.get(model.url, {}, { Origin: "null" }, function(response) {
// prints 200
console.log(response.status);
try {
options.success(JSON.parse(response.data));
} catch(e) {
console.error("JSON parsing error");
}
}, function(response) {
console.log(response.status);
console.log(response.error);
});
}else{
$.ajax({
type : 'GET',
url : model.url,
dataType : 'json',
success : function(data) {
console.log(data);
if(model instanceof Backbone.Collection){
model.reset(JSON.parse(JSON.stringify(data)));
}else{
model.set(JSON.parse(JSON.stringify(data)));
}
}
});
}
}
}