@yossiz אמר בSend Headers In Angular:
וואו, חבל, היית צריך לשאול הרבה יותר מוקדם...
השתדלתי לא להטריד.
@yossiz אמר בSend Headers In Angular:
הנה עשיתי stackblitz עם דוגמה מינימלית של שליחת headers באנגולר.
ניסיתי את זה ועוד הרבה אפשרויות ולא הועילו,
בשאלה הקודמת ניסיתי לחסוך,
אבל אם זה יעזור אז הנה מה שניסיתי,
רואטר בנוד:
const express = require('express');
const router = express.Router();
const {addSession, getListSessions, testSession, getAllInSession, getSession, deleteSession } = require('../controllers/sessions')
router.post('/addLogin/',addSession);
router.post('/list/:userId',getListSessions);
router.post('/test/',testSession);
router.get('/test/',testSession);
router.route('/')
.all(getAllInSession)
.post(getSession)
.delete(deleteSession)
module.exports = router;
קונטרולר בנוד:
module.exports = {
testSession: ((req, res) => {
console.log(req.headers)
res.status(200).json(req.headers);
})
}
שליחה באנגולר:
פונקציה בservice:
testSession(): Observable<any> {
const headers = {
'userId': '1000',
'strSession': 'Q9w762oUu3S#uwCqjZi@zZxDHl2bd~elw@nDCQVjQle9ACrkuf#JWutV#^S!Y21L7U1Rp8ZZGNlsJ!B1OPKqVbViNwgt4#sBuBCW'
}
return this.http.get<any>(`${this.settingsService.event.pathServer}sessions/test/`, { headers });
}
פונקציה בקומפוננטה:
test() {
this.sessionsService.testSession().subscribe(resulte => {
console.log(resulte);
},
err => {
console.log(err);
})
}
התוצאה:
קונסול בכרום:
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "//localhost:3000/sessions/test/", ok: false, …}
error: ProgressEvent
bubbles: false
cancelBubble: false
cancelable: false
composed: false
currentTarget: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "//localhost:3000/sessions/test/", __zone_symbol__loadfalse: Array(1), __zone_symbol__errorfalse: null, __zone_symbol__xhrScheduled: true, …}
defaultPrevented: false
eventPhase: 0
isTrusted: true
lengthComputable: false
loaded: 0
path: []
returnValue: true
srcElement: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "//localhost:3000/sessions/test/", __zone_symbol__loadfalse: Array(1), __zone_symbol__errorfalse: null, __zone_symbol__xhrScheduled: true, …}
target: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "//localhost:3000/sessions/test/", __zone_symbol__loadfalse: Array(1), __zone_symbol__errorfalse: null, __zone_symbol__xhrScheduled: true, …}
timeStamp: 159671.40500000096
total: 0
type: "error"
__proto__: ProgressEvent
headers: HttpHeaders
headers: Map(0) {}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure response for //localhost:3000/sessions/test/: 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: "//localhost:3000/sessions/test/"
__proto__: HttpResponseBase
zone-evergreen.js:2845 GET http://localhost:3000/sessions/test/ net::ERR_FAILED
קונסול בנוד:
OPTIONS /sessions/test/ 200 3.610 ms - 2
וב post:
אם אני משנה בservice מ:
return this.http.get<any>(`${this.settingsService.event.pathServer}sessions/test/`, { headers });
ל..
return this.http.post<any>(`${this.settingsService.event.pathServer}sessions/test/`, { userName: 'avi' }, { headers });
אני מקבל את אותו אובייקט שגיאה ועם אותו שגיאה בקונסול של כרום - רק מוחלף לpost ככה:
zone-evergreen.js:2845 POST http://localhost:3000/sessions/test/ net::ERR_FAILED
בקונסול בנוד אותו דבר:
OPTIONS /sessions/test/ 200 0.833 ms - 2
ולאומת זה אם אני מוריד את הheaders
משנה בservice לזה:
return this.http.post<any>(`${this.settingsService.event.pathServer}sessions/test/`, { userName: 'avi' });
או ל get ככה:
return this.http.get<any>(`${this.settingsService.event.pathServer}sessions/test/`);
אני מקבל בקונסול בכרום את התשובה הרצויה בלי שגיאה:
{host: "localhost:3000", connection: "keep-alive", content-length: "18", sec-ch-ua: ""Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"", accept: "application/json, text/plain, */*", …}
accept: "application/json, text/plain, */*"
accept-encoding: "gzip, deflate, br"
accept-language: "he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7"
connection: "keep-alive"
content-length: "18"
content-type: "application/json"
host: "localhost:3000"
origin: "http://localhost:4200"
referer: "http://localhost:4200/"
sec-ch-ua: ""Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99""
sec-ch-ua-mobile: "?0"
sec-fetch-dest: "empty"
sec-fetch-mode: "cors"
sec-fetch-site: "same-site"
user-agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
ובקונסול בכרום:
בpost:
{
host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '18',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
accept: 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
'content-type': 'application/json',
origin: 'http://localhost:4200',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:4200/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7'
}
POST /sessions/test/ 200 76.775 ms - 637
ובget:
{
host: 'localhost:3000',
connection: 'keep-alive',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
accept: 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
origin: 'http://localhost:4200',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:4200/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7'
}
GET /sessions/test/ 200 9.368 ms - 581
זה ממש יעזור