如何通过 AJAX 将 window.screen 传递给后端?
How can I pass window.screen to the back-end via AJAX?
如果我这样做:
window.screen
,我得到了:
Screen {availWidth: 3440, availHeight: 1415, width: 3440, height: 1440, colorDepth: 24, …}availHeight: 1415availLeft: -1057availTop: -1415availWidth: 3440colorDepth: 24height: 1440orientation: ScreenOrientation {angle: 0, type: 'landscape-primary', onchange: null}angle: 0onchange: nulltype: "landscape-primary"[[Prototype]]: ScreenOrientationpixelDepth: 24width: 3440[[Prototype]]: Screen
所以我假设,我可以访问它,所以我像这样将它发送到我的后端:
var data = {};
data.w = window.screen; //as soon as I did this, I can't never see request sent out in the network tab, if I changed that line to `data.w = 'test';`, only then I see request sending out again...
$.ajax({
method: 'POST',
url: '/tested-route',
crossDomain: true,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value'),
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache"
},
data: data,
success: function(response){
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
只有对象的可枚举属性会被 JSON.stringify
序列化。
将您关心的属性复制到一个新对象并对其进行编码,如果您想将它们编码为 JSON。
如果我这样做:
window.screen
,我得到了:
Screen {availWidth: 3440, availHeight: 1415, width: 3440, height: 1440, colorDepth: 24, …}availHeight: 1415availLeft: -1057availTop: -1415availWidth: 3440colorDepth: 24height: 1440orientation: ScreenOrientation {angle: 0, type: 'landscape-primary', onchange: null}angle: 0onchange: nulltype: "landscape-primary"[[Prototype]]: ScreenOrientationpixelDepth: 24width: 3440[[Prototype]]: Screen
所以我假设,我可以访问它,所以我像这样将它发送到我的后端:
var data = {};
data.w = window.screen; //as soon as I did this, I can't never see request sent out in the network tab, if I changed that line to `data.w = 'test';`, only then I see request sending out again...
$.ajax({
method: 'POST',
url: '/tested-route',
crossDomain: true,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value'),
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache"
},
data: data,
success: function(response){
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
只有对象的可枚举属性会被 JSON.stringify
序列化。
将您关心的属性复制到一个新对象并对其进行编码,如果您想将它们编码为 JSON。