網上多解決方法是加上
xhrFields: { withCredentials: true }
但是加的位置如果是錯誤的也沒用,所以記錄一下正確的跨域請求方式
$.ajax({
xhrFields: {
withCredentials: true
},
data: JSON.stringify(
this.prodectData
),
type: 'post',
dataType: 'json',
contentType: "application/json",
url: this.baseUrl + `/admin/addOrUpdateProduct`,
success: function (data) {
console.log(data)
if (confirm("操作成功,是否退出當前頁面?")) {
window.close();
}
}
});
$.ajax({
url: $then.baseUrl + `/admin/getProduct?id=` + urlid,
dataType: 'json',
xhrFields: {
withCredentials: true
},
processData: false,
type: 'get',
success: function (data) {
$then.prodectData = data
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});