Typescript CORS enabled but still getting CORS error [duplicate]
This question already has an answer here:
How does Access-Control-Allow-Origin header work?
13 answers
How to allow CORS?
25 answers
I have added a cors header in my Angular 7.0.5 project but i am still getting the following error when submitting a form and sending the form data to a 'back-end' (google script):
Access to XMLHttpRequest at 'https://script.google.com/blabla' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I am not receiving this error when i try this in Chrome with disabled security.
Might be a syntax error but i have been searching for a long time and tried a lot of things but i can't get it working.
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
import {ContactFormDto} from '../services/dto/contactform.dto';
const headers = new HttpHeaders()
.set('Content-Type', 'undefined')
.set('Access-Control-Allow-Origin', '*')
.set('Access-Control-Allow-Methods', 'POST')
.set('Access-Control-Allow-Headers', 'Origin')
.set('Access-Control-Allow-Credentials', 'true');
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.sass']
})
export class ContactComponent implements OnInit {
contactForm: FormGroup;
submitted = false;
MAIL_SCRIPT_URL =
'https://script.google.com/scriptlocation';
private contactFormDto: ContactFormDto;
constructor(private formBuilder: FormBuilder, private http: HttpClient) {
this.contactForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required],
message: ['', Validators.required],
});
}
onSubmit() {
this.submitted = true;
if (this.contactForm.valid) {
this.contactFormDto = new ContactFormDto(
this.contactForm.controls.name.value,
this.contactForm.controls.email.value,
this.contactForm.controls.message.value
);
this.http.post(
this.MAIL_SCRIPT_URL,
this.contactFormDto,
{headers}
).subscribe(data => {
console.log('Mail has been sent' + data);
}, error => {
console.log('Mail has not been sent ' + error);
});
console.log('name: ' + this.contactForm.controls.name.value + ' email: ' +
this.contactForm.controls.email.value + ' message: ' +
this.contactForm.controls.message.value);
}
}
ngOnInit() {
}
The 'backend' is a google script (runs when i post the form results to the url)
var TO_ADDRESS = "myaddress@gmail.com"; // where to send form data
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class
Logger
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
typescript cors
marked as duplicate by sideshowbarker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How does Access-Control-Allow-Origin header work?
13 answers
How to allow CORS?
25 answers
I have added a cors header in my Angular 7.0.5 project but i am still getting the following error when submitting a form and sending the form data to a 'back-end' (google script):
Access to XMLHttpRequest at 'https://script.google.com/blabla' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I am not receiving this error when i try this in Chrome with disabled security.
Might be a syntax error but i have been searching for a long time and tried a lot of things but i can't get it working.
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
import {ContactFormDto} from '../services/dto/contactform.dto';
const headers = new HttpHeaders()
.set('Content-Type', 'undefined')
.set('Access-Control-Allow-Origin', '*')
.set('Access-Control-Allow-Methods', 'POST')
.set('Access-Control-Allow-Headers', 'Origin')
.set('Access-Control-Allow-Credentials', 'true');
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.sass']
})
export class ContactComponent implements OnInit {
contactForm: FormGroup;
submitted = false;
MAIL_SCRIPT_URL =
'https://script.google.com/scriptlocation';
private contactFormDto: ContactFormDto;
constructor(private formBuilder: FormBuilder, private http: HttpClient) {
this.contactForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required],
message: ['', Validators.required],
});
}
onSubmit() {
this.submitted = true;
if (this.contactForm.valid) {
this.contactFormDto = new ContactFormDto(
this.contactForm.controls.name.value,
this.contactForm.controls.email.value,
this.contactForm.controls.message.value
);
this.http.post(
this.MAIL_SCRIPT_URL,
this.contactFormDto,
{headers}
).subscribe(data => {
console.log('Mail has been sent' + data);
}, error => {
console.log('Mail has not been sent ' + error);
});
console.log('name: ' + this.contactForm.controls.name.value + ' email: ' +
this.contactForm.controls.email.value + ' message: ' +
this.contactForm.controls.message.value);
}
}
ngOnInit() {
}
The 'backend' is a google script (runs when i post the form results to the url)
var TO_ADDRESS = "myaddress@gmail.com"; // where to send form data
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class
Logger
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
typescript cors
marked as duplicate by sideshowbarker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
use this google chrome extensionhttps://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
– ram12393
Nov 21 '18 at 12:34
Have you authorized preflight requests ? These areOPTIONSrequests made by the browser.
– trichetriche
Nov 21 '18 at 12:35
Also, as this has nothing to do with Angular, I highly suggest you update your code with your backend code, and the tags with the corresponding language, before your question gets edited (like, by me in five minutes)
– trichetriche
Nov 21 '18 at 12:35
What is your backed framework because there is nothing to do with angular
– Sheik Althaf
Nov 21 '18 at 12:35
add a comment |
This question already has an answer here:
How does Access-Control-Allow-Origin header work?
13 answers
How to allow CORS?
25 answers
I have added a cors header in my Angular 7.0.5 project but i am still getting the following error when submitting a form and sending the form data to a 'back-end' (google script):
Access to XMLHttpRequest at 'https://script.google.com/blabla' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I am not receiving this error when i try this in Chrome with disabled security.
Might be a syntax error but i have been searching for a long time and tried a lot of things but i can't get it working.
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
import {ContactFormDto} from '../services/dto/contactform.dto';
const headers = new HttpHeaders()
.set('Content-Type', 'undefined')
.set('Access-Control-Allow-Origin', '*')
.set('Access-Control-Allow-Methods', 'POST')
.set('Access-Control-Allow-Headers', 'Origin')
.set('Access-Control-Allow-Credentials', 'true');
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.sass']
})
export class ContactComponent implements OnInit {
contactForm: FormGroup;
submitted = false;
MAIL_SCRIPT_URL =
'https://script.google.com/scriptlocation';
private contactFormDto: ContactFormDto;
constructor(private formBuilder: FormBuilder, private http: HttpClient) {
this.contactForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required],
message: ['', Validators.required],
});
}
onSubmit() {
this.submitted = true;
if (this.contactForm.valid) {
this.contactFormDto = new ContactFormDto(
this.contactForm.controls.name.value,
this.contactForm.controls.email.value,
this.contactForm.controls.message.value
);
this.http.post(
this.MAIL_SCRIPT_URL,
this.contactFormDto,
{headers}
).subscribe(data => {
console.log('Mail has been sent' + data);
}, error => {
console.log('Mail has not been sent ' + error);
});
console.log('name: ' + this.contactForm.controls.name.value + ' email: ' +
this.contactForm.controls.email.value + ' message: ' +
this.contactForm.controls.message.value);
}
}
ngOnInit() {
}
The 'backend' is a google script (runs when i post the form results to the url)
var TO_ADDRESS = "myaddress@gmail.com"; // where to send form data
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class
Logger
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
typescript cors
This question already has an answer here:
How does Access-Control-Allow-Origin header work?
13 answers
How to allow CORS?
25 answers
I have added a cors header in my Angular 7.0.5 project but i am still getting the following error when submitting a form and sending the form data to a 'back-end' (google script):
Access to XMLHttpRequest at 'https://script.google.com/blabla' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I am not receiving this error when i try this in Chrome with disabled security.
Might be a syntax error but i have been searching for a long time and tried a lot of things but i can't get it working.
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
import {ContactFormDto} from '../services/dto/contactform.dto';
const headers = new HttpHeaders()
.set('Content-Type', 'undefined')
.set('Access-Control-Allow-Origin', '*')
.set('Access-Control-Allow-Methods', 'POST')
.set('Access-Control-Allow-Headers', 'Origin')
.set('Access-Control-Allow-Credentials', 'true');
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.sass']
})
export class ContactComponent implements OnInit {
contactForm: FormGroup;
submitted = false;
MAIL_SCRIPT_URL =
'https://script.google.com/scriptlocation';
private contactFormDto: ContactFormDto;
constructor(private formBuilder: FormBuilder, private http: HttpClient) {
this.contactForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required],
message: ['', Validators.required],
});
}
onSubmit() {
this.submitted = true;
if (this.contactForm.valid) {
this.contactFormDto = new ContactFormDto(
this.contactForm.controls.name.value,
this.contactForm.controls.email.value,
this.contactForm.controls.message.value
);
this.http.post(
this.MAIL_SCRIPT_URL,
this.contactFormDto,
{headers}
).subscribe(data => {
console.log('Mail has been sent' + data);
}, error => {
console.log('Mail has not been sent ' + error);
});
console.log('name: ' + this.contactForm.controls.name.value + ' email: ' +
this.contactForm.controls.email.value + ' message: ' +
this.contactForm.controls.message.value);
}
}
ngOnInit() {
}
The 'backend' is a google script (runs when i post the form results to the url)
var TO_ADDRESS = "myaddress@gmail.com"; // where to send form data
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class
Logger
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
This question already has an answer here:
How does Access-Control-Allow-Origin header work?
13 answers
How to allow CORS?
25 answers
typescript cors
typescript cors
edited Nov 21 '18 at 13:07
Joost Kaal
asked Nov 21 '18 at 12:32
Joost KaalJoost Kaal
678
678
marked as duplicate by sideshowbarker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by sideshowbarker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
use this google chrome extensionhttps://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
– ram12393
Nov 21 '18 at 12:34
Have you authorized preflight requests ? These areOPTIONSrequests made by the browser.
– trichetriche
Nov 21 '18 at 12:35
Also, as this has nothing to do with Angular, I highly suggest you update your code with your backend code, and the tags with the corresponding language, before your question gets edited (like, by me in five minutes)
– trichetriche
Nov 21 '18 at 12:35
What is your backed framework because there is nothing to do with angular
– Sheik Althaf
Nov 21 '18 at 12:35
add a comment |
use this google chrome extensionhttps://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
– ram12393
Nov 21 '18 at 12:34
Have you authorized preflight requests ? These areOPTIONSrequests made by the browser.
– trichetriche
Nov 21 '18 at 12:35
Also, as this has nothing to do with Angular, I highly suggest you update your code with your backend code, and the tags with the corresponding language, before your question gets edited (like, by me in five minutes)
– trichetriche
Nov 21 '18 at 12:35
What is your backed framework because there is nothing to do with angular
– Sheik Althaf
Nov 21 '18 at 12:35
use this google chrome extension
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en– ram12393
Nov 21 '18 at 12:34
use this google chrome extension
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en– ram12393
Nov 21 '18 at 12:34
Have you authorized preflight requests ? These are
OPTIONS requests made by the browser.– trichetriche
Nov 21 '18 at 12:35
Have you authorized preflight requests ? These are
OPTIONS requests made by the browser.– trichetriche
Nov 21 '18 at 12:35
Also, as this has nothing to do with Angular, I highly suggest you update your code with your backend code, and the tags with the corresponding language, before your question gets edited (like, by me in five minutes)
– trichetriche
Nov 21 '18 at 12:35
Also, as this has nothing to do with Angular, I highly suggest you update your code with your backend code, and the tags with the corresponding language, before your question gets edited (like, by me in five minutes)
– trichetriche
Nov 21 '18 at 12:35
What is your backed framework because there is nothing to do with angular
– Sheik Althaf
Nov 21 '18 at 12:35
What is your backed framework because there is nothing to do with angular
– Sheik Althaf
Nov 21 '18 at 12:35
add a comment |
1 Answer
1
active
oldest
votes
If you want Easiest way you can use plugins.
Allow-Control-Allow-Origin
URL LINK: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want Easiest way you can use plugins.
Allow-Control-Allow-Origin
URL LINK: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
add a comment |
If you want Easiest way you can use plugins.
Allow-Control-Allow-Origin
URL LINK: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
add a comment |
If you want Easiest way you can use plugins.
Allow-Control-Allow-Origin
URL LINK: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
If you want Easiest way you can use plugins.
Allow-Control-Allow-Origin
URL LINK: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
answered Nov 21 '18 at 12:36
Myco ClaroMyco Claro
369112
369112
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
add a comment |
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
this answer maybe help to other's they can easily see the answer.
– Myco Claro
Nov 21 '18 at 12:39
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
That's not an answer, and it won't help anyone with an issue. That's basically promoting a plugin, not answering a question.
– trichetriche
Nov 21 '18 at 12:43
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
I think this would only work on my pc, and it would not work when the application get's deployed. Or am i wrong here?
– Joost Kaal
Nov 21 '18 at 12:44
add a comment |
use this google chrome extension
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en– ram12393
Nov 21 '18 at 12:34
Have you authorized preflight requests ? These are
OPTIONSrequests made by the browser.– trichetriche
Nov 21 '18 at 12:35
Also, as this has nothing to do with Angular, I highly suggest you update your code with your backend code, and the tags with the corresponding language, before your question gets edited (like, by me in five minutes)
– trichetriche
Nov 21 '18 at 12:35
What is your backed framework because there is nothing to do with angular
– Sheik Althaf
Nov 21 '18 at 12:35